Hi,
Are you sure that your callback function gets called? Can you try to put debug code there, just to see if it is. I’ve had similar issues when I declared callback function incorrectly, e.g.
$defaults = array(
'get_callback' => test_callback(),
);
instead of:
$defaults = array(
'get_callback' => array( $this, 'test_callback' ),
);
when used in class scope.
Thanks
Thanks man.. Yes.. look
add_action( 'rest_api_init', function () {
register_rest_field( array('servicos', 'posts'),
'servico_status',
array(
'get_callback' => 'get_field_servico_status',
'update_callback' => 'slug_update_servicos',
'schema' => array(
'description' => 'My special field',
'type' => 'string',
'context' => array('view', 'edit')
)
)
);
});
function get_field_servico_status( $post, $field_name, $request ) {
return get_post_meta( $post[ 'id' ], $field_name, true );
}
function slug_update_servicos($value, $post, $field_name) {
if (!$value || !is_string($value)) {
return;
}
return update_post_meta($post->ID, $field_name, strip_tags($value));
}
Are you sure that slug_update_servicos() gets the arguments you’re checking and using in it? Can you try to debug all of the arguments by printing their values into error_log, i.e.:
error_log( print_r( $value, 1 ) );
error_log( print_r( $post, 1 ) );
error_log( print_r( $field_name, 1 ) );
Related question:
when a content is created, a HTTP 201 is returned and returned JSON contains the new data *including the custom field*.
I don’t understand how the update_callback could define the value given to the returned custom field.