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 6 replies - 1 through 6 (of 6 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 1 year, 5 months 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.

    Hi É nóis manolo,

    I recently switched from Yoast to RankMath and encountered the same issue you have described; based upon the theme being hardcoded to retrieve post metadata from the Yoast fields, but now it needs to grab the RankMath ones, instead.

    A quick review of the WordPress database has revealed the ‘Equilivant Mapping’ for the META_KEY parameters.
    SELECT * FROM 'wp_postmeta'

    Example: Use ‘rank_math_description’ instead of ‘_yoast_wpseo_metadesc’.
    Now when I update the ‘SEO Description’ in the WordPress dashboard, the theme accurately reflects the RankMath meta-data. The Yoast metadata is no longer accessed and just sits there as a kind of redundant backup, that should be cleaned out at some point (later).

    Problem.: <p><?php echo get_post_meta($post->ID, '_yoast_wpseo_metadesc', true); ?></p>
    Solution: <p><?php echo get_post_meta($post->ID, 'rank_math_description', true); ?></p>

    META_KEY			META_VALUE
    
    _yoast_wpseo_focuskw		WEEKNIGHT
    _yoast_wpseo_title		C_TIME_WEEKNIGHT1 in Formula Builder
    _yoast_wpseo_metadesc		What night of the week is it?
    _yoast_wpseo_metakeywords	WEEKNIGHT, C_TIME_WEEKNIGHT1, Formula Builder, BW Formula Builder
    _yoast_wpseo_linkdex		48
    _yoast_wpseo_canonical		/library/code/snippets/weeknight-in-formula-builder/
    
    rank_math_title			WEEKNIGHT in Formula Builder
    rank_math_description		What night of the week is it?
    rank_math_focus_keyword		WEEKNIGHT
    rank_math_canonical_url		/library/code/snippets/weeknight-in-formula-builder/
    rank_math_robots		a:1:{i:0;s:5:"index";}
    rank_math_rich_snippet		article
    rank_math_seo_score		59

    Part 2 – Dynamic Processing of Post Meta Description

    The above solution works when reading the raw data from the database. Cool, all good.

    However, it wasn’t until I went to leverage one of the cool features of replacement variables, like %excerpt_only%, which dynamically retrieves the post excerpt content as if it was the ‘SEO Description’ content, that I realised the above solution doesn’t work. This dynamic processing feature is great because it allows the content to be maintained once and used in two spots.

    Stored in Database: <p><?php echo get_post_meta($post->ID, 'rank_math_description', true); ?></p>
    Dynamic Processing: <p><?php echo RankMath\Post::get_meta( 'description', $post->ID ); ?></p>

    The dynamic processing code sample came from this existing RankMath article.
    https://rankmath.com/kb/filters-hooks-api-developer/#use-global-meta-description

    Hope this helps.
    John.

    • This reply was modified 6 months, 2 weeks ago by LocEnt. Reason: better code blocks
    • This reply was modified 6 months, 2 weeks ago by LocEnt. Reason: update table to select from
    • This reply was modified 6 months, 2 weeks ago by LocEnt. Reason: Added code sample solution for leveraging RankMath runtime processing as-well, not just reading the database

    Hi John,

    We really appreciate the input.

    I am sure others looking for this will find it super-useful.

    Thanks a ton.

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

The ticket ‘Insert the meta description in the template file’ is closed to new replies.