Yoast SEO 14.0: using Yoast SEO surfaces

In Yoast SEO 14.0 we've introduced a formal way of integrating Yoast SEO into your code. We've added what's called a surface called YoastSEO(). This surface gives easy access to lots of the features Yoast SEO has to offer. In this post I'll show you some of the different helpers that are now easily accessible if you're building an integration.

We'll cover:

Easily access SEO data for the current page

This is probably how most people would've integrated with Yoast SEO up till now: running one of our pieces of code and getting the title from it or the description. This was never really "easy". It always involved getting an instance of our WPSEO_Frontend class, something like this:


After that you had to do a bunch of stuff to get to our titles, and we never really made any of that easy. Now you can simply do this to get the title:

$title = YoastSEO()->meta->for_current_page()->title;

Need the description? Just as easy, you can even immediately echo it:

echo YoastSEO()->meta->for_current_page()->description;

The current_page surface exposes every bit of data we have on the current page, which all work in the same way; it's a long list:

$canonicalstringThe canonical URL for the current page.
$descriptionstringThe meta description for the current page, if set.
$titlestringThe SEO title for the current page.
$idstringThe requested object ID.
$site_namestringThe site name from the Yoast SEO settings.
$wordpress_site_namestringThe site name from the WordPress settings.
$site_urlstringThe main URL for the site.
$company_namestringThe company name from the Knowledge graph settings.
$company_logo_idintThe attachment ID for the company logo.
$site_user_idintIf the site represents a 'person', this is the ID of the accompanying user profile.
$site_representsstringWhether the site represents a 'person' or a 'company'.
$site_represents_referencearray|falseThe schema reference ID for what this site represents.
$breadcrumbs_enabledboolWhether breadcrumbs are enabled or not.
$schema_page_typestringThe Schema page type.
$main_schema_idstringSchema ID that points to the main Schema thing on the page, usually the webpage or article Schema piece.
$page_typestringThe Schema page type.
$meta_descriptionstringThe meta description for the current page, if set.
$robotsarrayAn array of the robots values set for the current page.
$googlebotarrayThe meta robots values we specifically output for Googlebot on this page.
$rel_nextstringThe next page in the series, if any.
$rel_prevstringThe previous page in the series, if any.
$open_graph_enabledboolWhether OpenGraph is enabled on this site.
$open_graph_publisherstringThe OpenGraph publisher reference.
$open_graph_typestringThe og:type.
$open_graph_titlestringThe og:title.
$open_graph_descriptionstringThe og:description.
$open_graph_imagesarrayThe array of images we have for this page.
$open_graph_urlstringThe og:url.
$open_graph_site_namestringThe og:site_name.
$open_graph_article_publisherstringThe article:publisher value.
$open_graph_article_authorstringThe article:author value.
$open_graph_article_published_timestringThe article:published_time value.
$open_graph_article_modified_timestringThe article:modified_time value.
$open_graph_localestringThe og:locale for the current page.
$open_graph_fb_app_idstringThe Facebook App ID.
$schemaarrayThe entire Schema array for the current page.
$twitter_cardstringThe Twitter card type for the current page.
$twitter_titlestringThe Twitter card title for the current page.
$twitter_descriptionstringThe Twitter card description for the current page.
$twitter_imagestringThe Twitter card image for the current page.
$twitter_creatorstringThe Twitter card author for the current page.
$twitter_sitestringThe Twitter card site reference for the current page.
$sourcearrayThe source object for most of this page data.
$breadcrumbsarrayThe breadcrumbs array for the current page.

Whether you need the OpenGraph description or the robots array, this has you covered. Get used to opening your favorite IDE, typing YoastSEO()->meta->for_current_page()-> and see the type hints for the exact bit of data you need.

For other pages?

You've probably guessed it when you saw the for_current_page() bit. You can do this for other pages and page types too. This would work, if post ID 2 exists:

YoastSEO()->meta->for_post( 2 )->title;

Even better, let's say you have the URL https://example.com/test-page/:

$title       = YoastSEO()->meta->for_url( 'https://example.com/test-page/' )->title;
$description = YoastSEO()->meta->for_url( 'https://example.com/test-page/' )->description;

If that URL doesn't exist in the Indexables table it'll return false.

Easy access to our helpers

Sometimes you need more than just the raw SEO data of a page. For instance, you need to know whether the current post type should be indexable at all. Well, our post_type helper can help with that:

YoastSEO()->helpers->post_type->is_indexable( get_post_type() )

This will return a simple boolean. If you'd rather have a list of indexable post types? You should use:

$public_post_types = YoastSEO()->helpers->post_type->get_public_post_types();

The same works for taxonomies:

YoastSEO()->helpers->taxonomy->is_indexable( 'category' );

There are quite a few of these helpers, and not all of them may be equally useful to you. But do have a look around in your IDE and see which ones we have to offer, this too is a rather large list!

2 Responses to Yoast SEO 14.0: using Yoast SEO surfaces

  1. Christoph Bratschi
    Christoph Bratschi  • 2 years ago

    I tried the following code but it crashes at the meta property call:

    echo 'YoastSEO: ' . (function_exists('YoastSEO') ? 'true':'false') . '';

    if (function_exists('YoastSEO')) {
    //var_dump(YoastSEO()->meta); //FIXME crash

    //FIXME meta does not exist!
    echo '' . esc_html(YoastSEO()->meta->for_post($post->ID)->description) . '';

    • Joost de Valk

      Which version did you try that code with? Did you try it with the latest version of the release branch?