How to add custom fields to Yoast SEO meta tags

Yoast SEO plugin allows you to set dynamic variables for its meta tags, such as %%title%% for post title or %%excerpt%% for post excerpt. If you’re an advanced WordPress users, who use custom fields to storing data or custom content, you might want to add these custom fields to Yoast SEO meta tags.

Using Yoast SEO template variables

For most of cases, the custom field you want to add content to Yoast SEO meta tags are text, or textarea fields. To get their, Yoast SEO provides template variables. These variables will be replaced by their value when viewing on the front end.

For custom fields, the template variable is %%<custom-field-name>%%, where <custom-field-name> is replaced by your custom field key.

For example, if you have a custom field brief_description , then you can enter %%brief_description%% to the post meta description. See the screenshot below:

Enter custom field variable to Yoast SEO meta tags

Note that after entering the text, Yoast SEO automatically transforms it into a highlighted term with purple background. Don’t worry if you don’t see the %% at the beginning and the end of the term. It’s already there, it’s just hid by Yoast SEO to make the term easier to read.

Now go to edit a post and enter something into the Brief Description field, and you’ll see it appears in the snippet preview after you save or update the post:

Custom field in Yoast SEO snippet preview

Unlike other template variables, Yoast SEO doesn’t automatically render the value of custom field into the snippet preview in real-time. You need to save or update your post to see it.

In addition, you can use multiple custom fields for the meta tags if you want. So, you can enter something like this: %%brief_description%% – %%review_count%% stars.

REGISTERING A CUSTOM TEMPLATE VARIABLE

The first step is registering a custom template variable for Yoast. Use the following code:

add_action( 'wpseo_register_extra_replacements', function() 
{     
     wpseo_register_var_replacement( '%%my_var%%', 'my_callback_function', 'advanced', 'Some help text' ); } 
);

The hook wpseo_register_extra_replacements is used to register a custom variable replacements (template variables). Inside the callback function, we use Yoast SEO’s function wpseo_register_var_replacement() function to register a new variable. This function has 4 parameters:

  • Variable name: The name of the variable to replace, i.e. ‘%%var%%‘, the surrounding %% are optional, name can only contain [A-Za-z0-9_-]. Note that your variable cannot start with cf_ or ct_ as they’re used by Yoast SEO for custom fields and custom taxonomies.
  • Callback function: Callback function to retrieve the replacement value for the variable.
  • Type of variable: ‘basic‘ or ‘advanced‘, defaults to ‘advanced‘.
  • Help text: Help text to be added to the help tab for this variable.

Here is the callback function to retrieve the replacement value for the variable:

function my_callback_function() {     
 
  $value = 'mysetting';     // Get my setting

   return $value;

}

Here are some examples for you:

Customizing Primary Listing Category on Single Listing Title / Meta

Step 1: Activate Wilcity Child Theme

Step 2: Put the following code to functions.php of Child Theme

add_action( 'wpseo_register_extra_replacements', function() {
wpseo_register_var_replacement( '%%listingCat%%', 'wilcityAddPrimaryCategoryToListingCallback', 'advanced');
} );

function wilcityAddPrimaryCategoryToListingCallback(){
$aDirectoryTypes = \WilokeListingTools\Framework\Helpers\GetSettings::getAllDirectoryTypes(true);
if ( !is_singular($aDirectoryTypes) ){
return '';
}
global $post;

$termID = \WilokeListingTools\Framework\Helpers\GetSettings::getPrimaryTermIDOfPost($post->ID, 'listing_cat');
if ( !empty($termID) ){
$oTerm = get_term($termID, 'listing_cat');
if ( !empty($oTerm) && !is_wp_error($oTerm) ){
return $oTerm->name;
}
}

return '';
}

Step 3: Adding %%listingCat%% to SEO title / Meta description

Customizing Primary Listing Location on Single Listing Title / Meta

Do the same Customizing Primary Listing Category but replacing

$oTerm = get_term($termID, 'listing_cat');

with

$oTerm = get_term($termID, 'listing_location');

and replacing

wilcityAddPrimaryCategoryToListingCallback

with

wilcityAddPrimaryLocationToListingCallback

Tagged:

Leave A Comment?