WP REST API format response

Since you’re filtering with the rest_prepare_{post_type} filter, you could restrict it to the WP_REST_Posts_Controller::get_items() callback, with the rest_{post_type}_query filter: add_filter( ‘rest_post_query’, function( $args ) { add_filter( ‘rest_prepare_post’, ‘api_remove_extra_data’, 12, 3 ); return $args; } ); where the post type is post. Note that in general we always want to return the filter value and I … Read more

How can I cache WordPress Rest API Response

You should create a new instance from WP_REST_Response to set the Cache-Control value. <?php register_rest_route(‘wp/v2’, ‘/your_endpoint’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘function_callback’, )); function function_callback($data) { $response = array( 1,2,3,4,5,6,7,8 ); $result = new WP_REST_Response($response, 200); // Set headers. $result->set_headers(array(‘Cache-Control’ => ‘max-age=3600’)); return $result; } Click here to get more info about directives.

WP REST api.wordpress.org discovery

https://api.wordpress.org is a custom PHP API and is not running WordPress. Thus, there’s no WP REST API that you could use for discovery. That URL has nothing to do with the REST API in core except for that redirect to the handbook. Unfortunately not all of the API endpoints are documented or either open sourced, … Read more

woocommerce product custom field

You should ad the custom field to product API response, try this (untested) add_filter( ‘woocommerce_rest_prepare_product’, ‘custom_products_api_data’, 90, 2 ); function custom_products_api_data( $response, $post ) { // retrieve a custom field and add it to API response $response->data[‘purchasing_cost’] = get_post_meta( $post->ID, ‘purchasing_cost’, true ); return $response; }

Can we assume that /wp-json/ will always be /wp-json/ under any circumstances when creating custom REST routes?

Sure they can! Actually the structure you used is assuming that url rewrites are on. Which may be or may be not. To assure the right endpoint, you must use get_rest_url function: https://developer.wordpress.org/reference/functions/get_rest_url/ So in your case, this would be: ‘get_rest_url’ => get_rest_url(null, ‘/block_help/v1/block_identi…’)

WordPress REST API – Modify JSON before importing

Using the rest_pre_dispatch hook is probably the most straightforward way to go, but you need to be careful about putting your new request data back into the WP_REST_Request object properly. You were reassigning the WP_REST_Request object to an array of request data. Those changes are not persisted because the parameter is not passed by reference. … Read more

Detect if REST API is running

I would use : if ( defined( ‘REST_REQUEST’ ) && REST_REQUEST ) {…} Source: https://github.com/WP-API/WP-API/issues/926#issuecomment-162920686 If you take a look to this source code on you will notice the constant is defined before processing anything, I would check for it instead of calling the mentioned function wp_is_json_request() Why? if headers were not set when you … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)