Last active
January 20, 2025 11:31
-
-
Save audrasjb/b9982bf21fdfbeedd39f298bf3a85bf7 to your computer and use it in GitHub Desktop.
Add Yoast reading time post meta before posts content
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
<?php | |
/** | |
* Plugin Name: Add Yoast reading time post meta before posts content. | |
* Author: Jb Audras | |
* Version: 0.1 | |
* Author URI: https://whodunit.fr | |
*/ | |
/** | |
* Gets Yoast reading time and add it before the content. | |
*/ | |
function jba_yoast_reading_time( $content ) { | |
global $post; | |
if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) || is_plugin_active( 'wordpress-seo-premium/wp-seo-premium.php' ) ) { | |
$yoast_estimated_time = YoastSEO()->meta->for_post( $post->ID )->estimated_reading_time_minutes; | |
// Because we don't want to show 0 minutes… | |
$yoast_estimated_time = $yoast_estimated_time > 0 ? $yoast_estimated_time : 1; | |
$content = '<p>Est. reading time : ' . $yoast_estimated_time . ' minutes.</p>' . $content; | |
} | |
return $content; | |
} | |
add_filter( 'the_content', 'jba_yoast_reading_time' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment