【发布时间】:2023-01-16 22:31:29
【问题描述】:
我正在使用 python 创建 wordpress 帖子还要照顾 YOAST 字段,使用 wordpress rest api。在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'}, }我找到一个代码 sn-p在这个link,这可能是一个有用的起点我在下面报告:
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' );我把上面的代码放在函数.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" },谢谢你们,
【问题讨论】:
标签: python wordpress-rest-api yoast