简体   繁体   English

Wordpress REST API - YOAST 字段的发布请求

[英]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 :我正在使用 python 创建 wordpress 后处理 YOAST 字段,使用 wordpress rest api。在YOAST 网站上我发现了这个声明

The Yoast REST API is currently read-only, and doesn't currently support POST or PUT calls to update the data. Yoast REST API 目前是只读的,目前不支持 POST 或 PUT 调用来更新数据。

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):同时,我想知道是否有一些解决方法可以通过邮寄请求更新 Yoast 字段,就像这样(偏离路线是行不通的):

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.我将上面的代码放在了function.php中,希望它是正确的地方。

How could I update all/some of the fields of YOAST by rest api post request?我如何通过 rest api post 请求更新 YOAST 的所有/部分字段? 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.根据 yoast 文档: Yoast REST API 目前是只读的,目前不支持 POST 或 PUT 调用来更新数据。 https://developer.yoast.com/customization/apis/rest-api/#can-i-use-this-api-to-update-data https://developer.yoast.com/customization/apis/rest-api/#can-i-use-this-api-to-update-data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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