Hot answers tagged

49

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): ...


21

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-...


18

Is this meant to be used on sites in production? Yes. Many sites has 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's responses have nothing to do with security, what can you do with a blank screen/read only access? ...


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 ...


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

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 = $...


8

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


7

If possible, only the examples shown in internet is: qod_remove_extra_data function ($ 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 [...


7

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; ...


6

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


6

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 ...


5

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'] <?php function my_awesome_func( WP_REST_Request $request ) { // You can access parameters via direct array access on the object: $param = $request['some_param']; ...


5

You can use $request->get_body()


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

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 ...


5

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. ...


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

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/...


4

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-...


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

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.


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' => ...


4

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, ...


4

I think it depends on the specifics on what your looking for, you can gain the most control by making your own route tough, the users route is more meant to list out users, not exactly search them as there is a pretty crazy amount of stuff you could be wanting to search based off of, and doing that all through parameters might get difficult. I am not saying ...


3

I found the answer, dropping it here in case someone need it. You need to be logged in as a user that has the edit_posts capability. With that capability, you also get access to the private query vars, including post_status. Taken from this similar issue.


3

This is how I did it, but I feel it could be better. For one thing, this results in HTTP 500... 403 would be preferable add_filter( 'json_authentication_errors', function( $authenticated ) { if( !$authenticated ) { return new WP_Error('Access Denied'); } }, 99 ); (I understand this'll work for Basic Auth too)


3

I ran into the same problem but for posts. I found how to OR or AND tags together here: https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters To get entries that have both L1 AND L2 AND L3 ; use plus (+) https://example.com/cms/wp-json/pages?filter[tag]=L1+L2+L3 In case somebody else comes along and wants to OR terms together I'll save you ...


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