How to customize Yoast Meta Description for custom post types or your pages and posts?

Recently I came across a requirement where I needed to customize the meta descriptions of properties – specifically their detail pages. And, since there were too many listings and the data was getting replaced every other week or month.. I needed to handle it dynamically like having a custom meta description template for the listing detail pages.

The WordPress setup was using Yoast SEO plugin which is one of the most popular and widely used SEO plugins in WordPress. So, after going through the Yoast available documents I found an action to just customize or add and manage your own custom variables which you can replace with your custom values related to property like its title, price, availability, phone, etc.

Here is an example of how I got it implement.

1. Create a method/function to retrieve your custom string or text that you would like to replace or show in the meta title, description, or keywords field.

function getSitePropertyTitle(){
     /*applying check to make queries or any actions only in the case of specific custom post types*/
     if ( 'listing' === get_post_type() ) {
            /*get your property title here*/
            return 'My custom new property title';

2. Use the Yoast SEO action to replace your custom variables

add_action('wpseo_register_extra_replacements', 'func_custom_yoast_variables');

3. Now, call the function you have specified in the action.

function func_custom_yoast_variables() {
       wpseo_register_var_replacement( '%%property_title%%', 'getSitePropertyTitle', 'advanced', 'Property title' );

Mention your custom variable, specify the callback function to retrieve the value.

And, last but the most important step. Add this custom variable in the Yoast configurations – Keywords, Title or Meta description fields.

In the above screenshot.. I have used 3 custom variables in the meta description field namely – %%property_title%%, %%property_status%% and %%property_agent_phone%%

So, you have to code your functions in a way to handle or replace these 3 variables.

Also, if you have noticed.. you can use these custom variables within your custom meta description statement.

Good Luck!

Leave a Reply

Your email address will not be published. Required fields are marked *