[Resolved] Creating a shortcode with yoast

Home Forums Support [Resolved] Creating a shortcode with yoast

Home Forums Support Creating a shortcode with yoast

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1891230
    nomadiceman

    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?

    #1891293
    Elvin
    Staff
    Customer Support

    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.

    #1891298
    nomadiceman

    This is awesome

    I understand everything apart from what I need to put in the (string)

    #1891304
    Elvin
    Staff
    Customer Support

    Ah it’s for making the output coming from YoastSEO().. into string characters.

    You can keep it that way.

    #1891323
    nomadiceman

    thank you! I will give that a try

    #1891405
    Elvin
    Staff
    Customer Support

    No problem. Let us know how it goes. 😀

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.