Search code examples
wordpresswordpress-rest-api

update_callback not calling proper function when sending post


So, here's my code so far:

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);
}

Now, I expect to have get_user_acf_fields function to be executed when I send GET request to /users/ endpoint, and update_user_acf_fields to be run when I send POST request to said endpoint. In both cases it executes former, get_user_acf_fields, function. What am I doing wrong here?


Solution

  • Maybe you didn't include the proper 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.

    — Read more on https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

    Here's your code that I modified and tested working on WordPress 4.9.5 with Advanced Custom Fields 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;
    }
    

    And here's the HTML and JS/AJAX I used for testing the PHP code above.