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

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

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.

Creating Application Password using REST API results in 401 regardless of JWT token

Because what you’re trying to do boils down to the fundamental question: How do I log in to WordPress using only the REST API? The answer: You can’t in stock/vanilla WordPress. /wp-json/wp/v2/users/me/application-passwords/ “message”: “You are not currently logged in.”, and /wp-json/wp/v2/users/1/application-passwords/ “message”: “Sorry, you are not allowed to create application passwords for this user.”, Are … Read more