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

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