Reading Time Variable for Yoast Titles and Descriptions
Adds a %%wpide_reading_time%% variable to Yoast SEO, so meta descriptions or social titles can say "5 min read".
Opens WPIDE on
Code
// Average reading speed.
$wpide_words_per_minute = 220;
add_action( 'wpseo_register_extra_replacements', function () use ( $wpide_words_per_minute ) {
if ( ! function_exists( 'wpseo_register_var_replacement' ) ) {
return;
}
wpseo_register_var_replacement( '%%wpide_reading_time%%', function () use ( $wpide_words_per_minute ) {
$post = get_post();
if ( ! $post ) {
return '';
}
$words = str_word_count( wp_strip_all_tags( strip_shortcodes( $post->post_content ) ) );
return sprintf( '%d min read', max( 1, (int) ceil( $words / max( 1, $wpide_words_per_minute ) ) ) );
}, 'advanced', 'Estimated reading time of the post, for example "5 min read".' );
} );
Notes
Type %%wpide_reading_time%% in any Yoast title or description field. Change the words per minute to suit your readers.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.