Yoast SEO 14.0: changing the Yoast Schema API
Due to the Yoast SEO indexable project using an entirely new namespaced Dependency Injection architecture, we’ve had to change some of our Schema API. All your existing integrations still work as we’ve made them backwards compatible, but we ask that you please update to the new code when you can. In this post I will explain the changes we made.
Overall, there are four important changes. All your old code still works, as we’ve added full backwards compatibility. However, that code will start throwing deprecation notices. The good news is, these changes are fairly simple and will make life easier in the long run.
- We’ve changed the interface from a PHP interface to an abstract class.
- Schema IDs for existing pieces should be gotten from a new class
Schema_IDs
. - We’ve changed how you generate Schema image tags.
- If you were extending our existing classes, they have moved.
WPSEO_Graph_Piece
becomes Abstract_Schema_Piece
If you were implementing your own graph pieces and adding them with the , you probably had something like this:
class YoastCon implements \WPSEO_Graph_Piece {
You should replace it with:
use \Yoast\WP\SEO\Generators\Schema\Abstract_Schema_Piece; class YoastCon extends Abstract_Schema_Piece {
This Abstract_Schema_Piece
is an abstract, and not an interface, because it has two public properties: $context
and $helpers
. These are filled magically and can be used to obtain important data.
WPSEO_Schema_IDs
becomes Schema_IDs
The Schema ID helper class has moved, it’s now called Schema_IDs
, full namespace Yoast\WP\SEO\Config\Schema_IDs
. This is simply a case of importing that class and then search & replacing all your code, so for instance WPSEO_Schema_IDs::PERSON_LOGO_HASH
becomes Schema_IDs::PERSON_LOGO_HASH
.
Schema image changes
Instead of doing this:
$image_schema_id = $context->canonical . '#product_image'; $image_helper = new \WPSEO_Schema_Image( $image_schema_id ); $schema_image = $image_helper->generate_from_attachment_id( $attachment_id );
You can now simply do this:
$image_schema_id = $this->context->canonical . '#product_image'; $image_helper = $this->helpers->schema->image->generate_from_attachment_id( $image_schema_id, $attachment_id );
The context, that is now of class Meta_Tags_Context
is automatically exposed under $this->context
. All the helpers you could need are under $this->helpers
. As you can see, generating an image can be done using the Yoast\WP\SEO\Helpers\Schema\Image_Helper
class, which is automatically available under $this->helpers->schema->image
. You’ll find that if you use a modern IDE like PHPStorm or Visual Studio Code, all of these auto expand for lots of coding convenience.
Note that the Schema ID for the image has become a parameter on the function call instead of on the class constructor.
Classes have moved
We’ve moved classes, according to the table below, and the signature of some of the functions within them have slightly changed, nine times out of ten to get a Meta_Tags_Context
parameter.
If you were, for instance, extending WPSEO_Person
, that is now called Yoast\WP\SEO\Generators\Schema\Person
. Extending Person
is as simple as this:
use Yoast\WP\SEO\Generators\Schema\Person; /** * Class Team_Member */ class Team_Member extends Person {
The old versus new class names:
Old class name | New class name |
---|---|
WPSEO_Graph_Piece |
Now an abstract class instead of an interface : Abstract_Schema_Piece |
WPSEO_Schema_Article |
Article |
WPSEO_Schema_Author |
Author |
WPSEO_Schema_Breadcrumb |
Breadcrumb |
WPSEO_Schema_Context |
Meta_Tags_Context automatically available under $this->context when you extend Abstract_Schema_Piece |
WPSEO_Schema_FAQ |
FAQ |
WPSEO_Schema_FAQ_Question_List |
Rolled up into FAQ |
WPSEO_Schema_FAQ_Questions |
Rolled up into FAQ |
WPSEO_Schema_HowTo |
HowTo |
WPSEO_Schema_IDs |
Schema_IDs |
WPSEO_Schema_Image |
Please use $this->helpers->schema->image |
WPSEO_Schema_MainImage |
Main_Image |
WPSEO_Schema_Organization |
Organization |
WPSEO_Schema_Person |
Person |
WPSEO_Schema_WebPage |
WebPage |
WPSEO_Schema_Website |
WebSite |
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
Webinar: How to start with SEO (November 19, 2024)
19 November 2024 Learn how to start your SEO journey the right way with our free webinar. Get practical tips and answers to all your questions in the live Q&A! All Yoast SEO webinars »
1 Response to Yoast SEO 14.0: changing the Yoast Schema API