Skip to main content

Yoast SEO: Surfaces API

In Yoast SEO 14.0 we 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.

Easily access SEO data for the current page

All of the SEO data for the current page can be easily accessed through our surface.

For example, this code provides the page's title as a variable:

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

This code immediately outputs the page's meta description:

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

This code outputs the estimated reading time for the current page.

echo (string) YoastSEO()->meta->for_current_page()->estimated_reading_time_minutes, " minutes";

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:

VariableTypeDescription
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_referencearrayfalse
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.
schemaarrayThe entire Schema array for the current page.
twitter_cardstringThe X card type for the current page.
twitter_titlestringThe X card title for the current page.
twitter_descriptionstringThe X card description for the current page.
twitter_imagestringThe X card image for the current page.
twitter_creatorstringThe X card author for the current page.
twitter_sitestringThe X card site reference for the current page.
sourcearrayThe source object for most of this page data.
breadcrumbsarrayThe breadcrumbs array for the current page.
estimated_reading_time_minutesintThe estimated reading time in minutes for the content.
post_authorstringThe name of the post author

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.

Deprecated properties

VariableTypeDescription
open_graph_fb_app_idstringThe Facebook App ID.

For other pages

Getting data for any page works in almost exactly the same way as getting data for the current page. You just need to provide an ID, or a URL.

E.g., get the canonical URL value for a post with an ID of 2.

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

E.g., get the title of a page with a URL of https://www.example.com/example-page/:

YoastSEO()->meta->for_url( 'https://www.example.com/example-page/' )->title;

NOTE: If a URL doesn't exist in our Indexables table, then this method will return false.

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 want 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!