REST API documentation problem – Modifying responses – custom fields
-
Hi,
I am really just wanting to highlight some confusing documentation I noticed while looking into registering custom endpoints for custom fields and post types.
I am also not sure if this is where I should be reporting this, if some other forums is more proper, please advise.
https://v2.wp-api.org/extending/modifying/
Honestly I found this very clear, great examples, well laid out. But at the top there is a warning to go and use the developer handbook instead as this may be out of date…But while this is fairly similar it is a lot less clear.
It also seems to have included some cutting and pasting from the older resource, completely out of context.
For example, at the bottom, this code:
<?php // The object type. For custom post types, this is 'post'; // for custom comment types, this is 'comment'. For user meta, // this is 'user'. $object_type = 'post'; $args1 = array( // Validate and sanitize the meta value. // Note: currently (4.7) one of 'string', 'boolean', 'integer', // 'number' must be used as 'type'. The default is 'string'. 'type' => 'string', // Shown in the schema for the meta key. 'description' => 'A meta key associated with a string meta value.', // Return a single value of the type. 'single' => true, // Show in the WP REST API response. Default: false. 'show_in_rest' => true, ); register_meta( $object_type, 'my_meta_key', $args1 );
has the accompanying text:
This example shows how to allow reading and writing of a post meta field. This will allow the spaceship field to be updated via a POST request to wp-json/wp/v2/posts/<post-id> or created along with a post via a POST request to wp-json/wp/v2/posts/.
However, the code in the developer handbook has no mention of a spaceship field, not anywhere on the modifying responses page in the developer handbook has any mention of said spaceship custom field….
The spaceship custom field comes from the example on the old resource, as follows:
add_action( 'rest_api_init', 'slug_register_starship' ); function slug_register_starship() { register_rest_field( 'post', 'starship', array( 'get_callback' => 'slug_get_starship', 'update_callback' => null, 'schema' => null, ) ); } /** * Get the value of the "starship" field * * @param array $object Details of current post. * @param string $field_name Name of field. * @param WP_REST_Request $request Current request * * @return mixed */ function slug_get_starship( $object, $field_name, $request ) { return get_post_meta( $object[ 'id' ], $field_name, true ); }
So I can only assume that the new developers handbook rest api documentation is pointing to an example in the old resource, that is suggesting that you should use the new resource, which doesn’t contain that example????
Seems a bit messy.
Jeff
- The topic ‘REST API documentation problem – Modifying responses – custom fields’ is closed to new replies.