wp-api-yoast
wp-api-yoast copied to clipboard
How to install plugin?
Hi,
Can you please help with installing and activating plugin?
Where do I need to add this code from plugin.php?
Download the zip file then install or extract it and put to plugin folder.
I've done everything as you said. But still don't have nothing in response.
I am using JSON API to retrieve all basic data for posts, but If there is possibility to get Yoast data for each post that will be perfect.
Any suggestion?
Yeah, I just installed this and it doesn't work either.
Same thing here. Installed the plugin but can't see the fields. @jmfurlott, any suggestions? @christianesperar, did you manage to make it work? I'm running WordPress 4.5.2
I managed to make this work. Disclaimer: My knowledge of PHP is very very basic.
I found this blog post and based on it made some modifications to the plugin. Pasting below the code that worked for me running WordPress 4.5.3, WP REST API 2.0-beta13.1 and Yoast SEO 3.3.2.
add_action( 'rest_api_init', function() {
register_api_field( 'post',
'yoast',
array(
'get_callback' => 'get_yoast',
'update_callback' => null,
'schema' => null,
)
);
});
function get_yoast( $object, $field_name, $request ) {
$yoastMeta = array(
'yoast_wpseo_title' => get_post_meta($object['id'], '_yoast_wpseo_title', true),
'yoast_wpseo_metadesc' => get_post_meta($object['id'], '_yoast_wpseo_metadesc', true),
'yoast_wpseo_focuskw' => get_post_meta($object['id'], '_yoast_wpseo_focuskw', true),
'yoast_wpseo_linkdex' => get_post_meta($object['id'], '_yoast_wpseo_linkdex', true),
'yoast_wpseo_metakeywords' => get_post_meta($object['id'], '_yoast_wpseo_metakeywords', true),
'yoast_wpseo_meta-robots-noindex' => get_post_meta($object['id'], '_yoast_wpseo_meta-robots-noindex', true),
'yoast_wpseo_meta-robots-nofollow' => get_post_meta($object['id'], '_yoast_wpseo_meta-robots-nofollow', true),
'yoast_wpseo_meta-robots-adv' => get_post_meta($object['id'], '_yoast_wpseo_meta-robots-adv', true),
'yoast_wpseo_canonical' => get_post_meta($object['id'], '_yoast_wpseo_canonical', true),
'yoast_wpseo_redirect' => get_post_meta($object['id'], '_yoast_wpseo_redirect', true),
'yoast_wpseo_opengraph-title' => get_post_meta($object['id'], '_yoast_wpseo_opengraph-title', true),
'yoast_wpseo_opengraph-description' => get_post_meta($object['id'], '_yoast_wpseo_opengraph-description', true),
'yoast_wpseo_opengraph-image' => get_post_meta($object['id'], '_yoast_wpseo_opengraph-image', true),
'yoast_wpseo_twitter-title' => get_post_meta($object['id'], '_yoast_wpseo_twitter-title', true),
'yoast_wpseo_twitter-description' => get_post_meta($object['id'], '_yoast_wpseo_twitter-description', true),
'yoast_wpseo_twitter-image' => get_post_meta($object['id'], '_yoast_wpseo_twitter-image', true)
);
return $yoastMeta;
}
Thanks @arikogan for this update.
Since you posted this Wordpress has renamed register_api_field to register_rest_field, so I had to change that to get it working.
Also, as it stands the code will only include Yoast metadata for posts. To have metadata appear in page responses, it's necessary to change post to page on line 2.
@jmfurlott if you are still maintaining this project would you consider including these updates to get the plugin working again?