Welcome Guest! Login? Checkout

Following on the previous post about adding a location and a date to the post URL automatically, we’re going to be looking at adding custom fields in post titles and descriptions. The sort you can see in Yoast Snippets. This will saving us time in optimising the post.

We are using Yoast WordPress SEO to build this feature. The reason being is that Yoast has a range of filters and functions that you can take advantage of this for your future projects. In my opinion as a former SEO, the strength of WordPress SEO over it’s competitors is not in it’s ability to do the basic stuff well, but the way in which you can automate SEO to a degree using custom code. In this example, we’ll be using a filter and a function to build new features.

The wpseo_register_extra_replacements action

The wpseo_register_extra_filter action is an action that allows you to register extra variable replacements. Variables in Yoast usually take the place of a keyword surrounded by four percentage signs – for example the %%title%% string is replaced with the title string. We will need to add a function to the action to register the variable replacement.

The wpseo_register_var_replacement Function

The wpseo_register_var_replacement is a function that will allow you to quite simply register a variable replacement. It’s a function that accepts 4 arguments, and returns a string.

The four arguments are:-

  • Variable – The variable string we will use to search for in the Title tag or description.
  • Callback – Function that will replace the variable with a string.
  • Type – Either Basic or Advanced. Defaults to Advanced. Not sure exactly what this does.
  • Label – The display label for the variable.

Putting it all together

If we are to put it all together, we have a custom field called “location” for the post type “talks”, and we are looking to turn %%talklocation%% and the %%talkdatemonth%% into the location and the date, respectively.

One Issue

One issue I’ve discovered has been whilst this is replaced on the front end, in the Dashboard area still will show the variable, affecting the title length.

This is an issue that has been reported, and hopefully will be fixed soon! But it does work on the front end.

 
 
 
WP Engine Managed WordPress Hosting

Comments

Polite Disclaimer: I am welcome, open and willing for corrections to be shared in the comments (with corrections being added to posts and credited), and the comments field should be used to promote discussion and make this post better. I do not know everything and if anybody finds a better way to do something, then by all means please share it below.

However, I'm unable to offer support to posts. The reason being is that WordPress has tens of thousands of plugins and millions of themes. As such, finding out exactly why code doesn't work with your setup is a long process. If you wish for me to look at your code, please use the priority support area.

Comments asking support will be left on the site, but there is no guarantee of answer.

    Leave a Reply

    Your email address will not be published.

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">
    *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.