Yoast SEO 14.0: adding metadata

In Yoast SEO 14.0, every meta tag that Yoast SEO outputs is output through a so called Presenter. In fact, all of them are extensions of the Abstract_Indexable_Presenter class, and you can easily add your own. Just make your own class that extends Abstract_Indexable_Presenter, and filter it in through the filter wpseo_frontend_presenters. Let me show you how!

Each implementation of Abstract_Indexable_Presenter must implement the two functions: get and present.

  • The get function should return the raw value of your meta (usually whatever is in the content property of the meta tag).
  • The present function should return the full meta tag.

Every Abstract_Indexable_Presenter class has 3 public properties, helpersreplace_vars and the presentation. You can use these to gather all data you need:

  • The presentation contains all data that Yoast SEO puts out. Your IDE should make it easy for you to find the values you need.
  • The helpers property 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 ).
  • The replace_vars property ensures you can use the $this->replace_vars( $string ) function to make use of our replacement variables in your own string if so desired.

Combined, this should lead to code that looks something like this:

use Yoast\WP\SEO\Presenters\Abstract_Indexable_Presenter;

/**
 * Adds a custom my:property.
 */
class My_Custom_Presenter extends Abstract_Indexable_Presenter {
	/**
	 * This output the full meta tag you want to add.
	 */
	public function present() {
		return '<meta property="my:property" content="' . esc_attr( $this->get() ) . '" />';
	}

	/**
	 * Returns the value of our new tag.
	 *
	 * @return string The value of our meta tag. 
	 */
	public function get() {
		return ( $this->presentation->open_graph_locale === 'nl_NL' ) ? 'Dutch' : 'Not dutch';
	}
}

If you want, you can go even one step further and make it even easier on yourself by implementing Abstract_Indexable_Tag_Presenter, which is even simpler to implement if it's a simple tag. This would be enough:

use Yoast\WP\SEO\Presenters\Abstract_Indexable_Tag_Presenter;

/**
 * Adds a custom my:property.
 */
class My_Custom_Presenter extends Abstract_Indexable_Tag_Presenter {

	/**
	 * The tag format including placeholders.
	 *
	 * @var string
	 */
	protected $tag_format = '<meta property="my:property" content="%s" />';

	/**
	 * Returns the value of our new tag.
	 *
	 * @return string The value of our meta tag.
	 */
	public function get() {
		return ( $this->presentation->open_graph_locale === 'nl_NL' ) ? 'Dutch' : 'Not dutch';
	}
}

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 by setting the $escaping class attribute of your presenter to 'url'. Because Abstract_Indexable_Tag_Presenter is itself inherited from Abstract_Indexable_Presenter all the tools you have there are available here too.

Regardless of which method you choose, you could then add your presenter through a filter:

/**
 * Adds our custom presenter to the array of presenters.
 *
 * @param array $presenters The current array of presenters.
 *
 * @return array Presenters with our custom presenter added.
 */
function add_my_custom_presenter( $presenters ) {
	$presenters[] = new My_Custom_Presenter();

	return $presenters;
}

add_filter( 'wpseo_frontend_presenters', 'add_my_custom_presenter' );


2 Responses to Yoast SEO 14.0: adding metadata

  1. Joe Gonzalez
    Joe Gonzalez  • 4 months ago

    Can we install even if we are running Yoast Premium?
    I don't want to lose my redirects.

    • Joost de Valk

      Certainly Joe! We’ll release both Free and Premium with the same code changes.