简体   繁体   English

使用 wordpress rest api 发送评论元

[英]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.

相关问题 WordPress REST API:评论和喜欢计数 - WordPress REST API: comment and like count 如何发送发布请求到WordPress的REST API有自定义字段? - How to send post request to Wordpress rest api have custom fields? 使用Wordpress REST API创建帖子时如何设置自定义元值 - How do I set a custom meta value when creating a post using the Wordpress REST api Angular - How to get posts, tags and comment count from Wordpress REST API using switchMap and then combine the resulting values using JavaScript? - Angular - How to get posts, tags and comment count from Wordpress REST API using switchMap and then combine the resulting values using JavaScript? 选项在 REST API 调用之前调用元 - OPTIONS call for meta before REST API call 如何使用 Wordpress REST api 将消息从 Vuejs 发送到 Contact-Form-7? - How to send message from a Vuejs to Contact-Form-7 with Wordpress REST api? 无法使用REST API使用Javascript将评论添加到Jira - Unable to PUT comment to Jira with Javascript using REST API Woocommerce REST API:将元数据添加到订单项和数据库 - Woocommerce REST API: Add meta data to line-item and to the database Sails REST API 和 MySQL:TypeError:Model.create(...).meta 不是函数 - Sails REST API and MySQL: TypeError: Model.create(…).meta is not a function WordPress REST API - 允许任何人发布 - WordPress REST API - Allow anyone to POST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM