Skip to content
WPCasts Mark

Supercharge Yoast with Custom Tags!

Alex Young | WordPress Nerd

Mechanic working on a muscle car with a supercharger

Sup, WordPress nerds! If you’ve watched my recent video, you’re likely eager to learn more about creating custom Yoast tags for your WordPress site. In this article, we’re going to dive deeper into the topic, walking you through the process step by step.

Video Version

Don’t forget to subscribe to my YouTube channel – WPCasts

Understanding Custom Yoast Tags

Custom Yoast tags are an incredibly useful tool for optimizing your site’s SEO. Essentially, they allow you to update the output of your SEO title and meta description. You can do this on an individual post basis, a per post type basis, or even per category.

You have the freedom to insert variables like the site title or the title of the post, and these will show up in search results. If you want to pull in information from elsewhere or replace what’s being generated by default, that’s entirely possible – and that’s exactly what we’re going to cover today.

Adding a New Variable

To add a new variable, you need to use the action wpseo_register_extra_replacements, which involves a few steps:

  • Create an anonymous function
  • Check if the function exists to avoid any issues in case Yoast is deactivated
  • Use the function wpseo_register_var_replacement provided by Yoast

This function requires a few arguments:

  • The string to be replaced (surrounded by double percent symbols)
  • The function to run your logic and return a string
  • Whether it’s advanced or basic (to be honest, the difference isn’t clear!)
  • Helper text to guide users
<?php

add_action('wpseo_register_extra_replacements', function(){
    if (function_exists ('wpseo_register_var_replacement')){
        wpseo_register_var_replacement('%%WPC%%', function(){
            return 'WPCasts - WordPress Tutorials for the Real World!';
        }, 'advanced', 'WPC Replacement' );
    }
});

Once you’ve done this, you can refresh your page and replace your previous variable with your new one by typing it between double percent symbols. Then, just update your page and refresh again. Voila! You’ve successfully added a new custom Yoast tag.

Modifying Existing Variables With Custom Yoast Tags

What if you want to change or append to an existing variable, like the title? Well, there’s a filter for that! Yoast provides the wpseo_replacements filter, which returns an array of replacements. Simply check if the variable you care about is set, and then modify it as you see fit.

To append to the title, you can insert the title and then add a pipe or another string after it. For example, you can have the title, a pipe, and then a new page replacement. This approach lets you modify an existing variable and output it again, opening up countless creative possibilities.

<?php

add_action('wpseo_replacements', function($replacements){
    if(isset($replacements['%%title%%'])){
        $replacements['%%title%%'] .= ' | NEW PAGE REPLACEMENT';
    }
    return $replacements;
});

With these techniques in your back pocket, you’re well on your way to mastering the art of custom Yoast tags. Whether you’re running a blog, a car dealership, or anything in between, these tools can help optimize your site’s SEO and draw in more visitors.

I hope this article was helpful! If you have any further questions or suggestions for future topics, please leave a comment below. See you in the next one!

Related Posts: