设置评论元 REST API WordPress [英] Set comment meta REST API WordPress

查看:27
本文介绍了设置评论元 REST API WordPress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 REST API 在 WordPress 中创建评论.但不能设置评论元.我不知道为什么,也许我错过了一点.所以请帮忙.

i create comment in WordPress with REST API. But can't set comment meta. I don't know why, maybe i missing a little bit. So please help.

代码:

// Data
var ajax_data = {
    author: user.user_id,
    author_email: user.user_email,
    content: comment,
    parent: parent,
    post: post_id,
    meta: {
        '_crating_speed': rating_speed,
        '_crating_price': rating_price,
    },
}

// Set header
$.ajaxSetup({
    headers: {
        'Authorization': "Bearer " + user.token,
    }
});

// AJAX
$.post( rest_base + '/comments', ajax_data ).done( function( response ) {  
    console.log(response);
}).fail( function( xhr, status, error ) {
    console.log( error);
}, 'json' );

_crating_speed_crating_price 未插入数据库.

The meta _crating_speed and _crating_price not inserted to database.

推荐答案

同样的问题,解决方案是使用 register_meta() 添加这些元字段,将元字段添加到评论的架构中.

Same issue, solution is to add these using register_meta() which will add the meta fields to the schema of the comment.

https://developer.wordpress.org/reference/functions/register_meta/

就我而言,以下方法有效:

In my case the following worked:

add_action('init', function() {
    register_meta('comment', 'stars', [
        'type' => 'number',
        'description' => __('Stars'),
        'single' => true,
        'show_in_rest' => true
    ]);
}, 10 , 0);

这篇关于设置评论元 REST API WordPress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆