How to add additional http header to a wp_error rest response

WP_REST_Response extends WP_HTTP_Response class, which has a header() method for setting headers. Using the rest_request_after_callbacks filter should allow setting the header, but will need to duplicate the functionality of WP_REST_Server->error_to_response(), which at time of writing calls rest_convert_error_to_response(), so that’s easy enough. Here’s what I think this will end up looking like (untested). add_filter( ‘rest_request_before_callbacks’, static … Read more

How to get attributes from a post requested via the JSON API?

Keep in mind that WordPress uses Backbonejs as its data model representation in their Javascript API Client. Since post.fetch() is an async function and returns a promise then you’d have to do your logic/logging after the fetch is complete. So you write: post.fetch().then( function( p ) { console.log( p ); } ); LINKS: fetch

REST API – filters not working inside plugin class

This is a problem: public function __contruct() it should be __construct, the S is missing, meaning there is no constructor and the add_filter calls never happen The function that registers the routes also never runs because the add_action call is commented out: //add_action( ‘rest_api_init’, array( $this, ‘extend_default_routes’ ) ); This means the constructor never runs, … Read more

Can’t expose custom field to REST API

I think the problem here is how you register the rest routes and how you return data for the single project endpoint. When you register the route and the callback like this. function single_project($data) { $post_ID = $data[‘id’]; return get_post($post_ID); } add_action(‘rest_api_init’, function () { register_rest_route( ‘project/v1’, ‘post/(?P<id>\d+)’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘single_project’, … Read more

Isn’t rest_do_request() filtered by pre_get_posts() filter?

Removing the conditionals for the set() calls will solve the problem. I suspect that the REST request is populating those parameters, and as such the set() calls do not occur. Untested: add_action( ‘pre_get_posts’, ‘test_pre_get_post_filter’ ); function test_pre_get_post_filter( $query ) { if ( is_admin() ) { return; } $query->set( ‘post_status’, array( ‘publish’ ) ); $query->set( ‘posts_per_page’, … Read more

WP Gutenberg – How to parse simple images?

When you return $post->post_content you’re returning the raw content from the database. Things like shortcodes, dynamic blocks and responsive image markup will not exist in the content. To prepare content for output you need to run it through the the_content filter: ‘content’ => apply_filters( ‘the_content’, $post->post_content ), That being said, there is already an endpoint … Read more

How to add fee_lines using woocommerce rest API v3?

Here is my code that working fine. “fee_lines”: [ { “name”: “7.5% Booking Fee”, “tax_status”: “taxable”, “tax_class”: “”, “total”: “26.02”, “total_tax”: “26.02”, “taxes”: [], “meta_data”: [] }, { “name”: “Waiter Tip (5%)”, “tax_status”: “taxable”, “tax_class”: “”, “total”: “8.00”, “total_tax”: “8.00”, “taxes”: [], “meta_data”: [] } ]

How can I get the number of items stored under a cache group

Here is an untested one-liner, counting the cache data array returned from the magic __get method, with a fallback to an empty array if the group key is not set: ‘numberOfEntries’ => count( $wp_object_cache->cache[$group] ?? [] ); Another approach is to consider bindTo to access the private cache array of the $wp_object_cache instance via closure.