Yoast SEO: Post meta in the WordPress REST API
From Yoast SEO 29.0, Yoast SEO registers its post meta for the WordPress REST API. For post types with custom-fields support, the keys are part of the meta property of the WordPress endpoints, for example /wp/v2/posts/<id>.
The block editor uses these keys to save the Yoast SEO values when the wpseo_disable_metabox_in_block_editor filter removes the Yoast SEO metabox. The registration doesn't depend on that filter.
Post types
Yoast SEO registers the keys for every post type that meets both of the following conditions:
- The post type is available in the REST API, which means that
show_in_restistrue. - The post type is public and viewable, which means that
publicistrueandis_post_type_viewable()returnstrue. To change which post types meet this condition, use thewpseo_accessible_post_typesfilter.
Post types that WordPress uses for its own data, such as synced patterns, navigation menus, and templates, aren't public, so they don't get the keys.
Keys
| Group | Keys |
|---|---|
| General | _yoast_wpseo_focuskw, _yoast_wpseo_title, _yoast_wpseo_metadesc, _yoast_wpseo_is_cornerstone, _yoast_wpseo_estimated-reading-time-minutes |
| Analysis scores | _yoast_wpseo_linkdex, _yoast_wpseo_content_score, _yoast_wpseo_inclusive_language_score, _yoast_wpseo_seo_title_score, _yoast_wpseo_meta_description_score |
| Advanced | _yoast_wpseo_meta-robots-noindex, _yoast_wpseo_meta-robots-nofollow, _yoast_wpseo_meta-robots-adv, _yoast_wpseo_bctitle, _yoast_wpseo_canonical, _yoast_wpseo_redirect |
| Social | _yoast_wpseo_opengraph-title, _yoast_wpseo_opengraph-description, _yoast_wpseo_opengraph-image, _yoast_wpseo_opengraph-image-id, _yoast_wpseo_twitter-title, _yoast_wpseo_twitter-description, _yoast_wpseo_twitter-image, _yoast_wpseo_twitter-image-id |
| Schema | _yoast_wpseo_schema_page_type, _yoast_wpseo_schema_article_type |
| Primary term | _yoast_wpseo_primary_<taxonomy>, for example _yoast_wpseo_primary_category |
The social keys are registered only when the matching social feature is on. Add-ons add their own keys. For the full list, see WPSEO_Meta::$meta_fields.
Rules
The following rules apply to every key:
- Type. Every value is a string. The REST API rejects other types, so send
"1"and not1. - Access. Reading and writing a key requires the
edit_postcapability for that post. - Advanced and Schema groups. If Restrict advanced settings for authors is on in the Yoast SEO settings under Site basics, then the keys in these two groups also require the
wpseo_edit_advanced_metadataor thewpseo_manage_optionscapability. - Defaults. For a key without a stored value, the response contains the default value of that key. Yoast SEO deletes a stored value that equals the default.
- Sanitization. Yoast SEO passes every value through
WPSEO_Meta::sanitize_post_meta().
Make your plugin work with and without the metabox
If your plugin stores its own values next to the Yoast SEO values, or reads the hidden form fields of the metabox, then make the following changes. The plugin then works whether the wpseo_disable_metabox_in_block_editor filter is on or off.
Register your fields with Yoast SEO
To add a field, use the add_extra_wpseo_meta_fields filter. Yoast SEO registers every field from that filter for the REST API, except a field that is defined with 'type' => null. That type marks an internal value that your code stores on the server. For a field in the general, advanced, schema, or social group, Yoast SEO also renders a hidden form field in the metabox.
A field that you add only through wpseo_metabox_entries_<tab> gets a hidden form field but no REST API registration, so the block editor can't save it when the metabox is off.
add_filter( 'add_extra_wpseo_meta_fields', 'my_plugin_add_yoast_meta_fields' );
/**
* Adds a field to the Yoast SEO post meta.
*
* @param array<string, array<string, array<string, string>>> $fields The extra field definitions, per group.
*
* @return array<string, array<string, array<string, string>>> The field definitions with the extra field.
*/
function my_plugin_add_yoast_meta_fields( $fields ) {
$fields['general']['my_plugin_field'] = [
'type' => 'hidden',
'default_value' => '',
];
return $fields;
}
Register the callback before plugins_loaded priority 14. The stored key is _yoast_wpseo_my_plugin_field.
Read and write values in JavaScript
To find out which storage is active, call window.yoast.editorModules.helpers.isRestMetaActive(). Yoast SEO doesn't load the script that provides this function on its own, so add yoast-seo-editor-modules to the dependencies of your script. The function exists from Yoast SEO 29.0, so check that it exists before you call it.
const isRestMetaActive = window.yoast?.editorModules?.helpers?.isRestMetaActive ?? ( () => false );
const META_KEY = "_yoast_wpseo_my_plugin_field";
const FIELD_ID = "yoast_wpseo_my_plugin_field";
/**
* Gets the value from the editor store or from the hidden form field.
*
* @returns {string} The value.
*/
function getValue() {
if ( isRestMetaActive() ) {
return wp.data.select( "core/editor" ).getEditedPostAttribute( "meta" )?.[ META_KEY ] ?? "";
}
return document.getElementById( FIELD_ID )?.value ?? "";
}
/**
* Sets the value in the editor store or in the hidden form field.
*
* @param {string} value The value.
*
* @returns {void}
*/
function setValue( value ) {
if ( isRestMetaActive() ) {
const meta = wp.data.select( "core/editor" ).getEditedPostAttribute( "meta" );
if ( ! meta || meta[ META_KEY ] === String( value ) ) {
return;
}
wp.data.dispatch( "core/editor" ).editPost( { meta: { [ META_KEY ]: String( value ) } } );
return;
}
const field = document.getElementById( FIELD_ID );
if ( field ) {
field.value = value;
}
}
Keep the following points in mind:
- Until the editor has loaded the post,
getEditedPostAttribute( "meta" )returns no value. If you write before that moment, your value overwrites the stored value. - Every call to
editPost()marks the post as changed and adds an undo step. Skip the call if the value didn't change. - For values that your code calculates, such as scores, don't use
editPost(). To add such values to the request at save time, use theeditor.preSavePostfilter.
Run server-side code after a save
When the metabox is off, the block editor saves the post through the REST API only. In a REST request:
- WordPress runs the
save_postandwp_insert_postactions before it saves the post meta, so code on those actions reads the Yoast SEO values from before the save. To run code after the save, use therest_after_insert_<post_type>action or thewpseo_saved_postdataaction. Thewpseo_saved_postdataaction runs after each REST update of a post, which includes the first save from the block editor. is_admin()returnsfalse, and WordPress doesn't load the files inwp-admin/includes. Load the admin files that your code needs.
Change Yoast SEO values with the WordPress meta filters
If your plugin changes Yoast SEO values with the WordPress meta filters, then follow these rules:
get_post_metadata. Return the type that WordPress asks for: a string if$singleistrue, and an array of strings if$singleisfalse. With another type, saves from the block editor can fail, whether thewpseo_disable_metabox_in_block_editorfilter is on or off.update_post_metadata. If you use this filter to store a value somewhere other than post meta, then returntrue. If the filter returnsfalse, the REST API reports a failed save to the editor.