Archived topic
Creating a shortcode with yoast
5 replies · Started by nomadiceman on August 10, 2021
Has anyone created a shortcode to display some of the variables from yoast seo plugin?
I’m trying to create a shortcode for the “Estimate time” to read the post
echo (string) YoastSEO()->meta->for_current_page()->estimated_reading_time_minutes, " minutes";
Anyone done something similar?
Hi there,
I believe this can help you in creating shortcodes.
https://docs.generatepress.com/article/creating-a-shortcode/
So for example:
add_shortcode( 'yoast_estimate_time', function() {
ob_start();
// Start your PHP below
echo (string) YoastSEO()->meta->for_current_page()->estimated_reading_time_minutes, " minutes";
// End your PHP above
return ob_get_clean();
} );
Replacing (string) on the echo line with your text wrapping in ''
With this PHP snippet, you can use [yoast_estimate_time]. This is assumes the line you've provided is correct.
This is awesome
I understand everything apart from what I need to put in the (string)
Ah it's for making the output coming from YoastSEO().. into string characters.
You can keep it that way.
thank you! I will give that a try
No problem. Let us know how it goes. :D