In my examples, I will be using a custom post type of “recipes”
In order to get a custom post type to show on the REST API (i.e. https://jmrowe.com/blog/wp-json/wp/v2/recipes ) , two options when using register_post_type can be set.
show_in_rest (boolean) (optional) Whether to expose this post type in the REST API.
rest_base (string) (optional) The base slug that this post type will use when accessed using the REST API. Default: $post_type
show_in_rest must be set to true
rest_base will be the “base” of the url when calling it in the rest API. So if nothing is set, the $post_type slug will be used.. i.e. recipes. If rest_base is changed to say “recipes-api” then to call it via the REST api it would look like: https://jmrowe.com/blog/wp-json/wp/v2/recipes-api
Sending the rest_url from WordPress to Javascript
wp_localize_script should be used to send the rest api url to javascript in a portable way such as:
|
1 2 3 4 5 6 |
wp_localize_script('registered-js-handle','site_variables', array('rest_url'=>rest_url('wp/v2/recipes-api/')) ); // Now the rest url for recipes-api can be used in javascript // such as console.log(site_variables.rest_url); |
Creating new fields in the REST responses
To add more/custom fields in the REST response. A function that does so must be added to the hook “rest_api_init” so..
|
1 |
add_action('rest_api_init','function_name_that_adds_custom_fields'); |
register_rest_field() is used to register/add the custom field in the rest response
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
add_action( 'rest_api_init', 'create_api_posts_meta_field' ); function create_api_posts_meta_field() { // register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() ) register_rest_field( 'post', 'post-meta-fields', array( 'get_callback' => 'get_post_meta_for_api', 'schema' => null, ) ); } // function below is "callback" which provides what the // value of the new rest field will be. // in this case, it simply adds all post meta for an ID // to the rest field 'post-meta-fields' // the callback gives you access to the post object // so you can retrieve the post id function get_post_meta_for_api( $object ) { //get the id of the post object array $post_id = $object['id']; //return the post meta return get_post_meta( $post_id ); } |
A good reference for using nonces in custom REST endpoints: https://viastudio.com/wordpress-rest-api-secure-ajax-calls-custom-endpoints/
One thing to note from this example is that WordPress suggest passing the nonce via the header and not the request content data. So, below is the better way of passing the nonce from js back to WordPress:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$.ajax( { url: site_variables.root + 'wp/v2/posts/1', method: 'POST', beforeSend: function ( xhr ) { // this is "safer" way to send the nonce via ajax rather then // in a $GET or $POST field. xhr.setRequestHeader( 'X-WP-Nonce', site_variables.nonce ); }, data:{ 'title' : 'Hello Moon' } } ).done( function ( response ) { console.log( response ); } ); |