2022 Developer Survey is open! Take survey.

Hot answers tagged

86 votes
Accepted

WP Rest API - How to get featured image

You can get it without plugins by adding _embedas param to your query /?rest_route=/wp/v2/posts&_embed /wp-json/wp/v2/posts?_embed
user avatar
71 votes

Get WP Navigation Menu from REST API V2

Since I don't like it myself when the top answer is "Install plugin X", here is how I solved it: Menus are currently not available in the WP Rest. So what you need to do is register your own custom ...
user avatar
  • 811
27 votes

Sending JSON string through wp_remote_post()

Try setting the data_format parameter in your request like so: $data = wp_remote_post($url, array( 'headers' => array('Content-Type' => 'application/json; charset=utf-8'), 'body' ...
user avatar
  • 2,929
26 votes
Accepted

WP Rest API: details of latest post including featured media url in one request?

Ah I just had this problem myself! And while _embed is great, in my experience it is very slow, and the point of JSON is to be fast :D I have the following code in a plugin (used for adding custom ...
user avatar
25 votes
Accepted

Why use wp_send_json() over echo json_encode()?

wp_send_json() handles all parts of returning content in an AJAX call. First off, it sets the content type of the returned content to application/json with the proper charset. Secondly, it ...
user avatar
  • 3,266
15 votes
Accepted

Building a custom REST API

TL;DR Yes, WordPress can certainly act as a backend for a mobile app. Yes, a page can act as a rest endpoint / interface. No, a theme template is not the right territory for the logic. Write your own ...
user avatar
15 votes

WP Rest API - How to get featured image

I would NOT use the better rest API plugin. It did add featured images to the rest api but it also broke it. This is the simplest solution I was able to find that actually worked. Add the following ...
user avatar
  • 301
12 votes
Accepted

Implementing Isomorphic JavaScript (React JS) in Wordpress?

I don't think using React.js without Node.js (or at least V8 or rhino etc) counts as isomorphic, as isomorphic means that you are building JavaScript to run in the browser AND on the server. ...
user avatar
12 votes
Accepted

How to use wp_localize_script in custom page template?

Based on the code provided it looks like you are misunderstanding how wp_localize_script works. The signature of the function looks like this: wp_localize_script( $handle, $name, $data ); Where $...
user avatar
  • 947
11 votes
Accepted

Allowing admin-ajax.php to receive "application/json" instead of "x-www-form-urlencoded"

it seems the latter only accepts x-www-form-urlencoded That's not completely true. WordPress admin-ajax.php takes the action from $_REQUEST['action'] and $_REQUEST is equal to: array_merge($_POST, ...
user avatar
  • 44.6k
10 votes

WP Rest API: details of latest post including featured media url in one request?

Just add the _embed query argument to your URL asking for the posts, and every post object, will include the _embedded.[wp:featuredmedia] object, which includes all the images, just like the /media/$...
user avatar
9 votes

WP JSON REST API (Ryan McCue) how to query posts with specific meta data with 'AND' relation?

Sorry for answering my own question but it may help some other devs too. I created this additional filter 'json_query_var-meta_query'that returns the necessary arguments. function adjustQrry($data){ ...
user avatar
9 votes
Accepted

Fetch All Posts (Including Those Using a Custom Post Type) With Wordpress API

I ended up extending the API like suggested in the comments to my question, although I was hoping there was a default route that fetched all posts of all post types. Apparently, there isn't. So here'...
user avatar
  • 1,188
9 votes

WP REST API only returning partial list of users

To anyone who might still be hitting this problem, here's a checklist: Make sure you are authenticated AND your user has the list_users capability. Example: When adding a custom role, I make sure ...
user avatar
  • 103
8 votes
Accepted

Can't get a JSON object in response to an Ajax request with wp_ajax

BODA82's answer helped, but eventually I realized that I should have replaced responseText with responseJSON method in my JavaScript code. In the example below I was storing the Ajax response results ...
user avatar
  • 1,734
8 votes

I want to create a custom slug in WordPress and output JSON. How do I do this?

This is the approach to add a rewrite rule for swapping out the template that WP wants to render (usually the 404 template) with something else. In your case you're just wanting to die with some JSON ...
user avatar
  • 206
8 votes
Accepted

Show popular post in another php website via WP REST JSON API

I will give you a small answer to your update, doing this with the WP API. The API have the possibilities to use the WP_Query like also in core, but about the get parameters in the url. A URL to pull ...
user avatar
  • 16.6k
8 votes
Accepted

Unset data in custom post type WordPress API (wp-json)

If possible, only the examples shown in internet is: function qod_remove_extra_data($data, $post, $context) { // We only want to modify the 'view' context, for reading posts if ($context !== '...
user avatar
8 votes
Accepted

How Do I Add User Custom Field to REST API Response?

I got it. Turns out the tutorial I was looking at was old and I was using the wrong WP function. I was using register_api_field but the correct one to use is register_rest_field. It goes like this... ...
user avatar
  • 1,591
8 votes

WP Rest API - How to get featured image

You can get the name of the image with this path: array_name._embedded['wp:featuredmedia']['0'].source_url
user avatar
  • 81
8 votes

WP Rest API v2 return posts with specific tag

The filter param was removed in WP4.7, you should be able to access tags via their ID. For example to get the tag with the ID of 7 you could use: http://YOUSIITE.DEV/wp-json/wp/v2/posts?tags=7 If ...
user avatar
8 votes
Accepted

How to get the path to current theme, but from a JS file?

You can use wp_localize_script to pass an object with the url to load before the file. and then in the file you can use it like testpage_obj.json_data wp_register_script('testpage', ...
user avatar
  • 1,723
7 votes

Get WP Navigation Menu from REST API V2

@Liren answer works well. However few beginners may not able to adjust the route. Here is the code that works well with WordPress Rest API v2 with minimum modification. Replace your menu name only in ...
user avatar
7 votes
Accepted

importing third party json feed as custom post type

If you want the D.I.Y. route... The post type in this example is set to 'post' but you can see where to change it. Here is a working example to pull the data ( first page in this example ), parse the ...
user avatar
  • 9,439
7 votes
Accepted

WP API V2 returning Invalid User ID

Wordfence blocks the User endpoint from the public. In settings there is a checkbox you can unselect to make it visible in the WP Rest API again.
user avatar
  • 282
7 votes
Accepted

How do I create a user using the new JSON api in 4.7?

The REST API included in WordPress doesn't actually have authentication built into it. If you do normal authentication in WordPress by logging in, then your browser will receive a set of cookies. If ...
user avatar
  • 32k
7 votes
Accepted

WP Rest API v2 filter and display latest post with specific tag

You can actually find extensive documentation on wordpress.org. But let's have a look at this particular example: https://demo.wp-api.org/wp-json/wp/v2/posts?per_page=5&tags=3 The 5 in there is ...
user avatar
  • 5,431
7 votes
Accepted

Get Image URL instead of Attachment Id in Rest API

You can modifiy REST API responses in themes functions.php like this. function ws_register_images_field() { register_rest_field( 'post', 'images', array( '...
user avatar
  • 504
7 votes

Add Category Name to REST API

The API responses are designed to only return the IDs. You can then fetch the full information for each term. Of course it would not be very performant to make dozens of requests. So instead, the REST ...
user avatar
6 votes

WP Rest API - How to get featured image

Take a look at a plugin called Better REST API Featured Image. It adds the featured image URL to the original API response.
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible