For simple websites, or when SEO is not a serious consideration, it's common for plugins and themes to simply output or echo meta tags into the
<head> of a document.
However, more advanced SEO often requires meta tags to have context, an awareness of the other meta tags on a page, or complicated internal logic. For example, the value of a canonical URL tag might be influenced by the presence or value of a meta robots tag. For sites running complex or multiple plugins and themes, it's also important to consider standardization and security. Managing these kinds of challenges becomes increasingly cumbersome without a framework.
That's why we provide a formal way of managing the construction and output of meta tags in the
<head> of each page. We use a
presenter (an extension of the
Abstract_Indexable_Presenter class) for each tag, which you can easily modify or extend.
By default, Yoast SEO ships with the following presenters that output meta tags.
|Presenter||Tag format||Filter||Deprecated from|
Editing existing meta tags
Sometimes you might run into a situation where you want to edit the output of one of the meta tags which are output by Yoast SEO. To achieve this, Metadata Presenters you should use the relevant filter for the presenter in question.
Notes on using presenters and filters
- All these filters expect a string to be returned.
- Some presenters don't have an associated filter; typically where it makes more sense to programmatically alter the values of the post/page in question (such as with
Please note These filters will be subject to change in the future as they are not yet represented through a Presenter class.
|Filter to manipulate the image size used for Open Graph sharing. |
If used, this defined size will always be enforced. The size is either a string value (i.e.
Below you will find a variety of examples that demonstrate some filters we provide.
Manipulate the meta robots tag output
Appending a string to a category's title
Adding meta tags
Adding your own meta tag(s) is as simple as creating your own class which extends
Abstract_Indexable_Presenter. To get started, you'll need to understand the following:
Each implementation of
Abstract_Indexable_Presentermust implement the two functions:
getfunction should return the raw value of your meta (usually whatever is in the
contentproperty of the meta tag).
presentfunction should return the full meta tag.
Abstract_Indexable_Presenterclass has 3 public properties,
presentation. You can use these to gather all data you need:
helpersproperty currently includes all our helper classes which you can use to, for example, determine whether or not something is an article post type in regards to schema, using
$this->helpers->schema->article->is_article_post_type( $post_type ).
replace_varsproperty ensures you can use the
$this->replace_vars( $string )function to make use of our replacement variables in your own string if so desired.
presentationcontains all data that Yoast SEO puts out. Your IDE should make it easy for you to find the values you need.
Combined, this should lead to code that looks something like this:
For simple tags, you can use
Abstract_Indexable_Tag_Presenter, which simply defines the string format and variable placeholder for the tag. A simple example would look like this:
This simpler format requires you to specify your tag format and replace where your value should be with
%s, as it will be sprintf'ed to that position. This assumes that your
get function returns a value that needs attribute escaping. If it's in fact a URL, you could change the escaping logic by setting the
$escaping class attribute of your presenter to 'url'.
Abstract_Indexable_Tag_Presenter is itself inherited from
Abstract_Indexable_Presenter all the tools you have there are available here too.
Registering your presenter
Regardless of which of the above methods you choose, you can then register your presenter through a filter:
Removing meta tags
Removing meta tags can be done by using the
wpseo_frontend_presenters filter. In the following example, we'll remove the canonical URL presenter from the list.