Hello,

Today I would like to discuss advanced development in WordPress while focusing on Filters and Actions with regards to the Yoast SEO Plugin.

For those who don’t know, Yoast is a powerful tool for managing your Search Engine Optimization (SEO) in WordPress.

I consider Yoast SEO’s core involvement to be the management of Meta Titles and Descriptions; however, Yoast includes a host of additional tools and features, such as XML Sitemaps, Social Meta Tags and a heck of allot more.

So let’s jump in!

Yoast SEO is one of the greatest tools for managing your website’s Meta Tags because it gives you the ability to not only control tags on Posts, Pages and Categories, but also on Custom Post Types and Taxonomies.

You can either manually write each Meta Tag by hand (recommended) or utilize an auto-generating engine based on your own Shortcode combination (Up, Down, Up, Down, B+A, B+A).

In fact, Yoast SEO can even utilize WordPress Custom Field Values. This becomes extremely helpful for both real estate and e-commerce based websites.

But what happens if you want to include a conditional statement for your Meta Tags?

For example, you’re building an e-commerce website, and some of your products have particular attributes and others don’t; it would be smart to have full control over your Meta Tags.

So, sadly, we need to bypass Yoast SEO and manually filter the Meta Tags while defining which section of WordPress we would like to manage.

For this you should already have a basic understanding of WordPress Filters and Actions.

Next, I recommend reviewing Yoast’s own API documentation. This will be quite helpful for the next part of this tutorial.

Let’s get ’er done!

First, let’s create a Function in functions.php.

function YourFunctionName

And pass a variable such as $title or $desc.

function YourFunctionName ($title)

Open and close your Function.

function YourFunctionName ($title) {

//I am soo lonely

}

Then close the Function by defining the Filter or Action.

add_filter( 'wpseo_title', 'YourFunctionName' );

Are you still following? Next we need to define your WordPress Conditional Statement and return your variable within the Function you have just created.

function YourFunctionName ($title) {

  if ( is_page('Listings') ) {
  	$title = 'Your Webpage Title';
  }
  return $title;
}

add_filter( 'wpseo_title', 'YourFunctionName' );

Presto. Save, upload and make sure you didn’t blow anything up.

This concludes today’s tutorial. If you have any questions, please leave a comment below.

As always, thanks for reading!

© Muller Consulting 2021