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:
WPSEO_Frontend::get_instance();
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:
Variable | Type | Description |
---|---|---|
$canonical |
string |
The canonical URL for the current page. |
$description |
string |
The meta description for the current page, if set. |
$title |
string |
The SEO title for the current page. |
$id |
string |
The requested object ID. |
$site_name |
string |
The site name from the Yoast SEO settings. |
$wordpress_site_name |
string |
The site name from the WordPress settings. |
$site_url |
string |
The main URL for the site. |
$company_name |
string |
The company name from the Knowledge graph settings. |
$company_logo_id |
int |
The attachment ID for the company logo. |
$site_user_id |
int |
If the site represents a ‘person’, this is the ID of the accompanying user profile. |
$site_represents |
string |
Whether the site represents a ‘person’ or a ‘company’. |
$site_represents_reference |
array|false |
The schema reference ID for what this site represents. |
$breadcrumbs_enabled |
bool |
Whether breadcrumbs are enabled or not. |
$schema_page_type |
string |
The Schema page type. |
$main_schema_id |
string |
Schema ID that points to the main Schema thing on the page, usually the webpage or article Schema piece. |
$page_type |
string |
The Schema page type. |
$meta_description |
string |
The meta description for the current page, if set. |
$robots |
array |
An array of the robots values set for the current page. |
$googlebot |
array |
The meta robots values we specifically output for Googlebot on this page. |
$rel_next |
string |
The next page in the series, if any. |
$rel_prev |
string |
The previous page in the series, if any. |
$open_graph_enabled |
bool |
Whether OpenGraph is enabled on this site. |
$open_graph_publisher |
string |
The OpenGraph publisher reference. |
$open_graph_type |
string |
The og:type. |
$open_graph_title |
string |
The og:title. |
$open_graph_description |
string |
The og:description. |
$open_graph_images |
array |
The array of images we have for this page. |
$open_graph_url |
string |
The og:url. |
$open_graph_site_name |
string |
The og:site_name. |
$open_graph_article_publisher |
string |
The article:publisher value. |
$open_graph_article_author |
string |
The article:author value. |
$open_graph_article_published_time |
string |
The article:published_time value. |
$open_graph_article_modified_time |
string |
The article:modified_time value. |
$open_graph_locale |
string |
The og:locale for the current page. |
$open_graph_fb_app_id |
string |
The Facebook App ID. |
$schema |
array |
The entire Schema array for the current page. |
$twitter_card |
string |
The Twitter card type for the current page. |
$twitter_title |
string |
The Twitter card title for the current page. |
$twitter_description |
string |
The Twitter card description for the current page. |
$twitter_image |
string |
The Twitter card image for the current page. |
$twitter_creator |
string |
The Twitter card author for the current page. |
$twitter_site |
string |
The Twitter card site reference for the current page. |
$source |
array |
The source object for most of this page data. |
$breadcrumbs |
array |
The 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!
Coming up next!
-
Event
WordCamp Netherlands 2024
November 29 - 30, 2024 Team Yoast is at Sponsoring WordCamp Netherlands 2024! Click through to see who will be there, what we will do, and more! See where you can find us next » -
SEO webinar
The SEO update by Yoast - October & November 2024 Edition
26 November 2024 Get expert analysis on the latest SEO news developments with Carolyn Shelby and Alex Moss. Join our upcoming update! 📺️ All Yoast SEO webinars »
I tried the following code but it crashes at the meta property call:
echo ‘YoastSEO: ‘ . (function_exists(‘YoastSEO’) ? ‘true’:’false’) . ”;
if (function_exists(‘YoastSEO’)) {
//debug
//var_dump(YoastSEO());
//var_dump(YoastSEO()->meta); //FIXME crash
//FIXME meta does not exist!
echo ” . esc_html(YoastSEO()->meta->for_post($post->ID)->description) . ”;
}
Which version did you try that code with? Did you try it with the latest version of the release branch?