WordPress 4.7 REST API endpoints

According to ticket #38373 the following endpoints will be supported in version 4.7. Let me quote Rachel Baker: REST API endpoints for your WordPress content. These endpoints provide machine-readable external access to your WordPress site with a clear, standards-driven interface, allowing new and innovative apps for interacting with your site. These endpoints support all of … Read more

Widget with random posts from a blog for external sites

To create a special output of random posts: Register an endpoint to the root of your blog. See A (Mostly) Complete Guide to the WordPress Rewrite API for details. Refresh the permalink settings. I would do this on (de)activation only. Hook into ‘template_redirect’ and return your output depending on the details of the requested endpoint. … Read more

How to Rewrite WordPress URL for a Plugin

Create an endpoint. In the callback function for the endpoint call your plugin functions internally, so the whole WordPress environment is available and you are still on the same domain, no matter where the plugin URL is. Plus, make sure not to flush the rewrite rules on every page load, use the (de)activation hook for … Read more

Make Two Views of Post Type Archive At Two URLs

Updated approach The first thought/suggestion I made actually – like you said – doesn’t work as I understood it. At least I tried it and couldn’t figure it out. That said, what you want is still achievable, but not by using a endpoint or at least not by making use of add_rewrite_endpoint(). However I figured … Read more

How can I get users email (and additional data) from the rest API?

To add the user’s email address in the REST API response, register an additional field to the user object and add the email address: register_rest_field( ‘user’, ‘user_email’, [ ‘get_callback’ => static function (array $user): string { return get_userdata($user[‘id’])->user_email; }, ] ); Please note, however, that this is strongly discouraged because anyone can then see the … Read more

REST API endpoint for elasticpress autosuggest

After some inspecting the WP_REST_Request, it turned out, that the get_body() method was the one I’m looking for. Anyhow, this is what I ended up with: add_action( ‘rest_api_init’, function() { register_rest_route( ‘ep’, ‘/as/’, [ ‘methods’ => \WP_REST_Server::CREATABLE, ‘callback’ => ‘ep_autosuggest’, ] ); } ); function ep_autosuggest( WP_REST_Request $data ) { // Elasticsearch PHP Client $client … Read more

WooCommerce: Can’t use wc_get_products for custom REST API endpoints

you missed something, when you get a product using wc_get_product it returns to you an abstract object, so if you need to get product do this $product = wc_get_product($product_id); return $product->get_data(); also you can use all the other functionalities too, such as: $product->get_status(); $product->get_gallery_image_ids(); …

Why is my custom API endpoint not working?

Maybe start with just GET. Your route looks weird as well. Try just: register_rest_route(‘my-project/v1’, ‘/action/’, [ ‘methods’ => WP_REST_Server::READABLE, ‘callback’ => ‘api_method’, ]); And your callback is not returning a valid response. Let your callback look more like this: $data = [ ‘foo’ => ‘bar’ ]; $response = new WP_REST_Response($data, 200); // Set headers. $response->set_headers([ … Read more

In Gutenberg, now that withAPIData is being deprecated, how can I do an async call to a custom endpoint?

It’s worth noting that the answer above is out of date. The data store should now look like this: const actions = { setUserRoles( userRoles ) { return { type: ‘SET_USER_ROLES’, userRoles, }; }, receiveUserRoles( path ) { return { type: ‘RECEIVE_USER_ROLES’, path, }; }, }; const store = registerStore( ‘matt-watson/secure-block’, { reducer( state = … Read more