[英]send comment meta with wordpress rest api
I'm trying to send a comment with wordpress rest api and it works, but I can't send meta data I put the data in "meta" object while sending post request but the data won't save in the database I have exactly the problem that is mentioned in this post: Set comment meta REST API WordPress but nobody answered that.我正在尝试使用 wordpress rest api 发送评论并且它有效,但是我无法发送元数据 我在发送发布请求时将数据放入“元”对象中,但数据不会保存在我拥有的数据库中这篇文章中提到的问题: 设置评论元 REST API WordPress但没有人回答。
var commentData = JSON.stringify({ post: postId, author_name: nameVal, content: textVal, phone:phoneVal, meta:{ phone:phoneVal, } }); var ACTION_URL = apiUrl+'comments'; fetch(ACTION_URL, { method: 'post', headers: { 'Content-Type': 'application/json', }, body: commentData, }) .then((response) => { console.log(response.json()); }) .catch(error => { console.error('Error:', error) });
in wordpress documentation https://developer.wordpress.org/rest-api/reference/comments/#schema-meta says that you have to put meta data in meta object but while the response is ok and the comment will be submitted successfully the "phone" meta data will not be saved in the database.在 wordpress 文档中https://developer.wordpress.org/rest-api/reference/comments/#schema-meta说你必须将元数据放在元对象中,但是虽然响应正常并且评论将成功提交“电话”元数据不会保存在数据库中。
Same issue, solution is to add these using register_meta() which will add the meta fields to the schema of the comment.同样的问题,解决方案是使用 register_meta() 添加这些,这会将元字段添加到评论的架构中。
https://developer.wordpress.org/reference/functions/register_meta/ https://developer.wordpress.org/reference/functions/register_meta/
In my case the following worked:在我的情况下,以下工作:
add_action('init', function() {
register_meta('comment', 'phone', [
'type' => 'string',
'description' => __('Phone'),
'single' => true,
'show_in_rest' => true
]);
}, 10 , 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.