简体   繁体   中英

Wordpress REST API - Post Request for YOAST fields

I'm using python to create wordpress post taking care also of the YOAST fields , using the wordpress rest api. On YOAST website I found this statement :

The Yoast REST API is currently read-only, and doesn't currently support POST or PUT calls to update the data.

At the same time, I'm wondering if there is some workaround to be able to update the Yoast fields by post request, something like this (that off-course is not working right know):

post = {
    'title'    : 'My title',
    'content'  : 'This is my first post created using rest API Updated',   
    'yoast_head_json': {'title': 'This field should be UPDATED by POST REQUEST'},
}

I found a code snippet at this link , that maybe would be a useful starting point and I report it below:

class YoastUpdateController extends WP_REST_Controller {
    public function register_routes() {
        register_rest_route( 'wp/v2/', '/action/', array(
            'methods'  => 'GET',
            'callback' => [$this, 'update_yoast_meta']
        ));
    }

    function update_yoast_meta($data) {
        $postID = $_GET['postID'];
        $metadesc = $_GET['metaDesc'];
        if ($postID && $metadesc) {
            $this->add_to_yoast_seo($postID, $metadesc);
        }
    }

    function add_to_yoast_seo($post_id, $metadesc){

        $ret = false;
        $updated_desc = update_post_meta($post_id, '_yoast_wpseo_metadesc', $metadesc);
        if($updated_desc){
            $ret = true;
        }
        return $ret;
    }
}

function register_yoast_update_controller() {
    $controller = new YoastUpdateController();
    $controller->register_routes();
}

add_action( 'rest_api_init', 'register_yoast_update_controller' );

I placed the above code in function.php , I hope it is the right place.

How could I update all/some of the fields of YOAST by rest api post request? Below some fields (Eg title, description...)

  "yoast_head_json": {
    "title": "Post 1 - MyWebsite",
    "description": "Meta description added in backend",
    "robots": {
      "index": "index",
      "follow": "follow",
      "max-snippet": "max-snippet:-1",
      "max-image-preview": "max-image-preview:large",
      "max-video-preview": "max-video-preview:-1"
    },

Thank you all,

According to yoast documentation: The Yoast REST API is currently read-only, and doesn't currently support POST or PUT calls to update the data. https://developer.yoast.com/customization/apis/rest-api/#can-i-use-this-api-to-update-data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM