Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove Reading Time WP From Yoast og:desc
/**
* 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