update_callback 发送时没有调用正确的函数 post

update_callback not calling proper function when sending post

到目前为止,这是我的代码:

add_action( 'rest_api_init', 'rest_api_user_meta_fields' );

function rest_api_user_meta_fields() {
    // register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
    register_rest_field( 'user', 'grad', array(
           'get_callback'       => 'get_user_acf_fields',
           'update_callback'    => 'update_user_acf_fields',
           'schema'             => null,
        )
    );
}

function get_user_acf_fields( $object ) {
    //get the id of the post object array
    $grad = get_field('grad', 'user_32');
    error_log($object);
    return $grad;
}

function update_user_acf_fields( $value, $object, $field_name ) {
    error_log($value);
    error_log($field_name);
    error_log($object);
}

现在,我希望在向 /users/ 端点发送 GET 请求时执行 get_user_acf_fields 函数,并且在发送 update_user_acf_fields 时执行 运行 POST 请求到所述端点。在这两种情况下,它都执行前一个 get_user_acf_fields 函数。我在这里做错了什么?

$_GET、$_POST 和 UPDATE 与其余部分不同 API。由于更新不一定是语言的构造,它只是意味着当一个请求通常是一个 $_POST 带有一个特定的 "Update" header 时,它应该调用你的更新函数。

也许您没有包含正确的 nonce

For developers making manual Ajax requests, the nonce will need to be passed with each request. The API uses nonces with the action set to wp_rest. These can then be passed to the API via the _wpnonce data parameter (either POST data or in the query for GET requests), or via the X-WP-Nonce header. If no nonce is provided the API will set the current user to 0, turning the request into an unauthenticated request, even if you’re logged into WordPress.

— 阅读更多关于 https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

这是我在 WordPress 4.9.5 上使用高级自定义字段 4.4.12 修改和测试的代码。

add_action( 'rest_api_init', 'rest_api_user_meta_fields' );

function rest_api_user_meta_fields() {
    // register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
    register_rest_field( 'user', 'grad', array(
           'get_callback'       => 'get_user_acf_fields',
           'update_callback'    => 'update_user_acf_fields',
           'schema'             => [
             'description' => 'User grad blah',
             'type'        => 'string',
           ],
        )
    );
}

// $user_arr is an `array` of the user data; e.g. `id`, `username`, and `name`.
// $field_name is the name of the ACF custom field; i.e. in this case, it's `grad`.
function get_user_acf_fields( $user_arr, $field_name ) {
    // Get the value of the 'grad' custom field.
    $grad = get_field($field_name, 'user_' . $user_arr['id']);
    error_log(var_export( $user_arr, true ));
    error_log($field_name);
    //return $grad;
    return $field_name . ';' . $grad . ';' . $user_arr['id'];
}

// $field_value is the value of the ACF custom field; i.e. in this case, it's `grad`.
// $user_obj is an `object` of the user data; e.g. `id`, `username`, and `name`.
function update_user_acf_fields( $field_value, $user_obj, $field_name ) {
    // Get the value of the 'grad' custom field.
    $grad = get_field($field_name, 'user_' . $user_obj->ID);

    if ( $grad !== $field_value ) {
      update_field( $field_name, $field_value, 'user_' . $user_obj->ID );
    }

    error_log($field_value);
    error_log($field_name);
    error_log(var_export( $user_obj, true ));

    return true;
}

here's the HTML and JS/AJAX我用来测试上面的PHP代码。