I am trying to update Yoast Seo meta field with wordpress rest api.
I installed on my wordpress https://rathankalluri.com/yoast-seo-wordpress-api/ code - as I didn't found any plugin that updates Yoast Seo field.
This Function is added to functions.php
function wp_yoast_rest( WP_REST_Request $request ){
$post_id = $request ['post_id'];
$title = $request['seo_title'];
$desc = $request['seo_desc'];
$meta_title = $request['meta_title'];
$image_url = $request['img_url'];
$keywords = $request['keywords'];
$title_keys = array("_yoast_wpseo_opengraph-title","_yoast_wpseo_twitter-title");
$desc_keys = array("_yoast_wpseo_opengraph-description","_yoast_wpseo_twitter-description", "_yoast_wpseo_metadesc");
$img_keys = array("_yoast_wpseo_opengraph-image","_yoast_wpseo_twitter-image");
add_post_meta($post_id, '_yoast_wpseo_title', $meta_title);
foreach ($title_keys as $meta_key){
$meta_value = $title;
add_post_meta( $post_id, $meta_key, $meta_value);
}
foreach ($desc_keys as $meta_key){
$meta_value = $desc;
add_post_meta( $post_id, $meta_key, $meta_value);
}
foreach ($img_keys as $meta_key){
$meta_value = $image_url;
add_post_meta( $post_id, $meta_key, $meta_value);
}
//Yoast doesnt allow keywords so adding it as a custom post meta
add_post_meta($post_id, 'keywords', $keywords);
return "Success";
}
add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v2', '/yoast', array(
'methods' => 'POST',
'callback' => 'wp_yoast_rest',
) );
} );
My request looks like -
post = {'image_url': 'http://google.com',
'keywords': '',
'meta_title': 'Meta Title',
'post_id': 1,
'seo_desc': 'Test Description',
'seo_title': 'Seo Title'}
requests.post("http://example.com/wp-json/wp/v2/yoast", headers = headers, data = post)
but no one of the keys is updated ...
Am I doing something wrong?
some one have a working way to update seo values in wordpress using the rest api ?