Skip to content

Instantly share code, notes, and snippets.

@rathankalluri
Last active December 19, 2019 21:48
Show Gist options
  • Save rathankalluri/6b5aa9e68d0dc89c44e77ffe9764155f to your computer and use it in GitHub Desktop.
Save rathankalluri/6b5aa9e68d0dc89c44e77ffe9764155f to your computer and use it in GitHub Desktop.
Yoast wp-json API
<?php
// ..... add the following in wordpress functions.php file
/**
* Yoast Rest API Customized
*/
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',
) );
} );
// .... other code in functions.php
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment