Yoast SEO is a great plugin for optimizing your site for SEO. It is important that your articles are providing the right information to social media when being shared. Facebook uses Open Graph meta tags to show the correct information. In this tutorial, you’ll learn how to change the Yoast SEO Open Graph Meta through code.
The team behind Yoast SEO did a great job using WordPress Plugin API to make their plugin extensible. If you want to learn a bit about that, I have written an article on 5 Ways to Make your WordPress Plugin Really Extensible.
This plugin uses the class
WPSEO_OpenGraph to add the Yoast SEO Open Graph meta tags. When changing the Open Graph meta, you should hook into
This can be used to assign a different language if you want it. Maybe you’re using a different locale on your site by default but want to change the locale for a particular article? Let’s see how we can do that now:
We are checking for the ID of the article we want to change the language. Since this article is assumed to be written in Croatian, we will set it to be a valid Facebook tag for Croatian Language.
The type will tell Facebook that the URL that we are sharing is about an eBook or any type of a book.
Here we are again checking againts an ID. Since that ID is actually an eBook landing page, we will tell to Facebook that we are sharing information about an eBook.
For all types, please visit this page: https://developers.facebook.com/docs/reference/opengraph#object-type.
Title is the one of the most important tags. This can change how people will react to your shared content. Let’s say we have a product that was already sold to more than 100 people. We want to tell that in a title!
Pretty awesome, right?
This is another Yoast SEO Open Graph meta that we should use to get attention from people on Social Media. We are already using it when we create a custom meta description for our site. But what if we want to have a different description on social media?
You can also change the URL. I assume this is something you won’t change that much, but you could change the URL. Just be careful that the URL you are putting here is having the same meta tags defined before. Otherwise, Facebook will pull some different meta data from that URL.
For this example, I did not change the URL completely. I have just added another query parameter to it.
This is probably the most interesting part since we are all very visual. That is why many sites are creating specific images just for social media to get our attention.
Yoast SEO Open Graph image meta allows us to put more than one image.
If you look closely, I am not using the
add_filter but instead
add_action. That is because, Yoast SEO allowed us to add other images. We can ofcouse, filter some of the images that are being added through options. That can be done like this:
Other Yoast SEO Open Graph Meta Tags
There are some other tags that we could change:
wpseo_opengraph_admin– Site Owner (if App ID is not present)
wpseo_opengraph_site_name– Site Name
wpseo_opengraph_author_facebook– Facebook Profile URL
wpseo_opengraph_show_publish_date– True/False to show the publish date
Dynamic Open Graph Tags
Yoast SEO gives us another, deeper, layer where we can hook into and change other data. Some of the present filters here are the hardcoded ones that we can use. But, we can also change them differently and get access to other tags that are set without the filters.
That is done by the method
Be sure to check other methods to see how you can change them. Here are some of the examples for filters you can use:
Be sure to check other tags and methods here: https://github.com/Yoast/wordpress-seo/blob/trunk/frontend/class-opengraph.php
Have you ever changed the Yoast SEO Meta tags yourself? Or if you have even worked with the Open Graph, please do share your experience in the comments below.