Skip to content

Instantly share code, notes, and snippets.

@jasonyingling
Last active June 11, 2021 12:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonyingling/5917dc97b302ca37abce7ceb93a7f4b8 to your computer and use it in GitHub Desktop.
Save jasonyingling/5917dc97b302ca37abce7ceb93a7f4b8 to your computer and use it in GitHub Desktop.
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