Wordpress REST API-发布YOAST字段请求



我正在使用python创建wordpress post,同时使用wordpress rest api处理YOAST字段。在YOAST网站上,我发现了以下声明

Yoast REST API当前是只读的,并且当前不是支持POST或PUT调用来更新数据。

同时,我想知道是否有一些变通方法可以通过发布请求更新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'},
}

在这个链接上发现了一个代码片段,这可能是一个有用的起点并且我在下面报告它:

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' );

我把上面的代码放在function.php中,我希望它是正确的位置。

如何通过rest api post请求更新YOAST的所有/部分字段?某些字段下方(例如标题、说明…(

"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"
},

谢谢大家,

根据yoast文档:yoast REST API当前为只读,目前不支持POST或PUT调用来更新数据https://developer.yoast.com/customization/apis/rest-api/#can-i-use-the-api更新数据

最新更新