Created
February 27, 2025 14:17
-
-
Save audrasjb/7e27b04b2ea7a9a4bba327a2fa423997 to your computer and use it in GitHub Desktop.
Adds Yoast reading time post meta in a shortcode: [yoast-reading-time]
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: Yoast Reading Time Shortcode | |
* Description: Adds Yoast reading time post meta in a shortcode. | |
* Author: Jb Audras @ Whodunit | |
* Version: 0.1 | |
* Author URI: https://whodunit.fr | |
*/ | |
/** | |
* Gets Yoast reading time and add it in a shortcode: [yoast-reading-time] | |
*/ | |
function jba_yoast_reading_time_shortcode() { | |
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 = sprintf( | |
__( 'Estimated reading time: %s minutes.', 'who-yoast-reading-time-shortcode' ), | |
$yoast_estimated_time | |
); | |
return $content; | |
} | |
} | |
add_shortcode( 'yoast-reading-time', 'jba_yoast_reading_time_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment