Hot answers tagged

65

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 endpoint and then just call that route from your application that needs it. So you would include something like this (in your functions.php, plugin, wherever): ...


26

The WP Rest API sends the total count(found_posts) property from WP_Query. in a header called X-WP-Total. FOR POSTS: you can make a call to posts endpoint of the REST API http://demo.wp-api.org/wp-json/wp/v2/posts The value for posts count is returned in the header as X-WP-Total. Below is a sample response from the hosted demo Access-Control-Allow-...


20

Is this meant to be used on sites in production? Yes. Many sites have been already using it. Is there a security risk to allowing endpoints to be viewed by anyone, such as /wp-json/wp/v2/users/ which shows all users registered to the site? No. Server responses have nothing to do with security, nothing you can do against a blank screen or read only ...


12

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. Specifically, using WordPress certainly means you aren't doing isomorphic javascript (its PHP software). What you could do is use WordPress as a REST API server, and ...


10

You can call the REST API methods to prepare your output in the same way that the plugin does by default, this will also allow any plugins to tie into the output as you have used the ACF plugin as seen in your example output. The WP_REST_Posts_Controller class has the following in its get posts method $posts_query = new WP_Query(); $query_result = $...


9

You can use $request->get_json_params() which will return an array of key => values. With these conditions(possibly a few more): The client sending the request has Content-Type: application/json in the header There is a raw body like {"option":"siteColor","value":"ff0000"}. https://developer.wordpress.org/reference/classes/wp_rest_request/...


9

I had a quick look and here's how the headers are set in the WP_REST_Posts_Controller::get_items() method: $response = rest_ensure_response( $posts ); // ... $response->header( 'X-WP-Total', (int) $total_posts ); $response->header( 'X-WP-TotalPages', (int) $max_pages ); // ... return $response; where: $total_posts = $posts_query->found_posts; ...


8

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 content from Post Status would look like this: http://example.com/wp-json/posts To pull content with WP_Query parameters you’re used to, you could do it like ...


8

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 !== 'view' || is_wp_error($data)) { return $data; } // Here, we unset any data we do not want to see on the front end: unset($data['author']);...


8

In a previous answer is was able to access the data in a custom endpoint using $parameters = $request->get_query_params(); Check the query params for option $parameters['option'] Two options for getting the body of the request: $body = $request->get_body(); $body_params = $request->get_body_params(); Example <?php function my_awesome_func( ...


8

In case it's not clear how to actually access the header (with the post count) from the Ajax request, following is how to do it with jQuery .get. The key is that the callback function has an optional parameter containing the request, which includes the headers. $.get( 'http://demo.wp-api.org/wp-json/wp/v2/posts', function( data, status, request ) { numPosts ...


8

See if your url is correct. Example: website.com/wp-json/wp/v2/posts/?categories=3&per_page=‌​50


7

Is it possible to allow only authorized users to access an endpoint? It is possible to add a custom permission callback to your API endpoint which requires authentication to view the content. Unauthorized users will receive an error response "code": "rest_forbidden" The simplest way to do this is to extend the WP_REST_Posts_Controller. Here's a very simple ...


7

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.


6

You can use $request->get_body()


6

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 the number of posts you want returned. The 3 is the id of the tag you want to limit the posts too. If you want to limit to a category use categories instead. ...


6

This code will add categories_names field to wp rest api response: function wpse_287931_register_categories_names_field() { register_rest_field( 'project', 'categories_names', array( 'get_callback' => 'wpse_287931_get_categories_names', 'update_callback' => null, 'schema' => null, ...


6

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 to add the list_users capability. The user should also be logged in (what authenticated means) when making the request. By default, only users who have ...


5

There is a navigation menu plugin extension for the REST API v2: https://wordpress.org/plugins/wp-api-menus/


5

@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 wp_get_nav_menu_items() function. If menu name and slug does not work (Return false), use Menu ID (visible in Dashboard while editing that Menu). function ...


5

Adding a custom endpoint is pretty straightforward. I also modified the url to look more like http://example.com/wp-json/namespace/v2/posts?filter[meta_value][month]=12&filter[meta_value][year]=2015 function wp_json_namespace_v2__init() { // create json-api endpoint add_action('rest_api_init', function () { // http://example.com/wp-...


5

OK, i find my answer. Just ask it here and as dear Charles sayed, they answered me, so fast, the answer is: By default, WP-API v1 takes priority over v2. If v1 is installed and activated, then v2 routes are inaccessible. To mitigate this, you'll need to register either v1 or v2 to a different base than wp-json. Both have filters to make this more ...


5

Yes, there is a way to get that information programmatically. WordPress Version Info The WordPress version exists as a global, but you can also get it using the get_bloginfo() function: global $wp_version; echo $wp_version; // OR $wordpress_version = get_bloginfo( 'version' ); echo $wordpress_version; Plugin Info For retrieving plugin info, use the ...


5

I understand that having less data being downloaded improves the experience to the end user, but my question is related to the actual server performance, would the response be faster? Would adding _fields[]=id&_fields[]=title, etc... to the above url improve server performance? No it would not, and for several reasons. When you ask for a post it ...


4

I think we're not far away from being able to achieve this in a pretty sensible way. The WordPress.com website has already moved over to a JavaScript application powered by a REST API with Node.js and React. Moving the core WordPress software over to newer technologies like this will take time, but if you add plugins such as WordPress REST API you should ...


4

Make sure that signature request is handled correctly: http://oauth1.wp-api.org/docs/basics/Signing.html If you're using POSTMAN you can set it to OAuth 1.0 under Authorization and then select the options Add empty params to signature Encode OAuth signature Save helper data to request Here's a detailed tutorial I wrote on using OAuth 1 and Postman with ...


4

What version of the REST API are you using? If you're using the version that's bundled with WordPress 4.4 (ie, v2), you'll need to change your url to something like http://example.com/wp-json/wp/v2/posts. Reference WP API version 2 docs


4

If we look at the WP_REST_Controller::get_collection_params() method, we can see the minimum is 1 and the maximum is 100: 'per_page' => array( 'description' => __( 'Maximum number of items to be returned in result set.' ), 'type' => 'integer', 'default' => 10, 'minimum' => 1, '...


4

I'm the author of the node-wpapi library, thanks for checking it out. Unfortunately we do not currently support any external authentication scheme out of the box, because WordPress itself does not ship with any authentication scheme other than the cookie/nonce option (which doesn't work with external apps, as you describe). There are plugins for ...


4

Well, there is a way to update post meta. You have to add the update_callback when registering rest field. See the example below: function rest_api_player_meta() { register_rest_field('sp_player', 'player_meta', array( 'get_callback' => 'get_player_meta', 'update_callback' => 'update_player_meta', 'schema' => ...


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