Last active
June 11, 2021 12:22
-
-
Save jasonyingling/5917dc97b302ca37abce7ceb93a7f4b8 to your computer and use it in GitHub Desktop.
Remove Reading Time WP From Yoast og:desc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A Function to remove the auto generated Reading Time from Yoast og:desc | |
* | |
* This hooks into Yoast's `wpseo_opengraph_desc` filter and uses a regular | |
* expression to remove the reading time for the post | |
* | |
* @see Reading Time WP Plugin | |
* @link https://wordpress.org/plugins/reading-time-wp/ | |
* | |
* @param string $ogdesc The excerpt grabbed by Yoast for output. | |
* @return string The $ogdesc with the Reading Time string removed. | |
*/ | |
function remove_reading_time_from_yoast( $ogdesc ) { | |
//First we get the options set on this version of Reading Time WP | |
$rtwp_options = get_option('rt_reading_time_options'); | |
$rtwp_label = $rtwp_options['label']; | |
$rtwp_postfix = $rtwp_options['postfix']; | |
$rtwp_singular = $rtwp_options['postfix_singular']; | |
// Next we build a regular expression using those options | |
$rtwp_regex = $rtwp_label . '(.*?)(' . $rtwp_postfix . '|' . $rtwp_singular . ')'; | |
// Finally we use `preg_replace` to replace the reading time with an empty string | |
$ogdesc = preg_replace('/' . $rtwp_regex . '/', '', $ogdesc); | |
return $ogdesc; | |
} | |
add_filter( 'wpseo_opengraph_desc', 'remove_reading_time_from_yoast' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment