Insert the meta description in the template file

  • Hi. I’d like to know that is possible to use a php code to insert the meta description in the my template files. For example, I have used this with yoast, just inserting the code <?php echo WPSEO_Meta::get_value(“metadesc”); ?> im my single page instead a excerpt.

    Now that I changed to Rankmath, I had to take off this code but it is necessary to me have back the description.

    Thank you so much.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,

    Thank you for contacting the support.

    You can import all the data from your previous SEO plugin from Dashboard >> Rank Math >> Import & Export >> Plugin Importers.

    Rank Math plugin also has a filter which you could use to change the description for certain articles on your site:

    
    /**
     * Allow changing the meta description sentence.
     *
     * @param string $description The description sentence.
     */
    add_filter( 'rank_math/frontend/description', function( $description ) {
      if ( is_single() ) {
        global $post;
        return get_post_meta( $post->ID, '_meta_key', true );
      }
      return $description;
    });
    

    Add this code in your theme’s function.php file to dynamically change the description on single pages.

    Hope that helps. Thank you.
    ​​​​

    • This reply was modified 4 months, 2 weeks ago by  Pratik.

    Sorry, I think you didn’t understand (or me in your answer). I already imported all the meta descriptions of my posts.

    Before with the Yoast, I showed in the posts (single.php) the meta description after the title, like an excerpt. To do that, I used the php code <?php echo WPSEO_Meta::get_value(“metadesc”); ?>

    But now, with Rankmath, this code broked my site, it just works with yoast, so I took of it. I’d like to know there is some way to show the meta description again in my single posts with Rankmath.

    Tks!

    Hello,

    Do you want to show the description stored in <?php echo WPSEO_Meta::get_value(“metadesc”); ?> meta?

    If so, please add below code in your theme’s functions.php file:

    
    /**
     * Allow changing the meta description sentence.
     *
     * @param string $description The description sentence.
     */
    add_filter( 'rank_math/frontend/description', function( $description ) {
      if ( is_single() ) {
        global $post;
        return get_post_meta( $post->ID, '_yoast_wpseo_metadesc', true );
      }
      return $description;
    });
    

    The _yoast_wpseo_metadesc is the key where yoast stores the meta description.

    If this is not what you wanted, can you add a link to the posts in the Sensitive Data Section where the code broked?

    Looking forward to helping you. Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this ticket.