Skip to content

Instantly share code, notes, and snippets.

@audrasjb
Created February 27, 2025 14:17
Show Gist options
  • Save audrasjb/7e27b04b2ea7a9a4bba327a2fa423997 to your computer and use it in GitHub Desktop.
Save audrasjb/7e27b04b2ea7a9a4bba327a2fa423997 to your computer and use it in GitHub Desktop.
Adds Yoast reading time post meta in a shortcode: [yoast-reading-time]
<?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