How to remove rest api link: in http headers?

The output is generated by the rest_output_link_header(). This function is used in two actions, wp_head and template_redirect in default-filters.php:@line234. You can remove the function from those hooks to remove the output you wanted to remove. Put the following codes in your theme’s functions.php to achieve the desired result. remove_action( ‘wp_head’, ‘rest_output_link_wp_head’, 10); remove_action( ‘template_redirect’, ‘rest_output_link_header’, … Read more

Can I use REST-API on plain permalink format?

Yes you can. Just add the rest_route query parameter. So https://wordpress.org/wp-json/ would become https://wordpress.org/?rest_route=/ Or https://wordpress.org/wp-json/wp/v2/ would become https://wordpress.org/?rest_route=/wp/v2 to give you a more complete example. So you’re wondering how to decide which one to use? Worry no more, there’s a function for that: get_rest_url() Another option is the fact that by default there is … Read more

Add media with WP-Rest-API v2

SO! This is fun. Keep in mind the WP-API is still very, very much a work-in-progress. Content-Disposition I found an issue reported on the WP-API issue queue about Content-Disposition. This is a required header for posting new media content and there are some very, very strict requirements when it comes to providing this in the … Read more

Nonce retrieved from the REST API is invalid and different from nonce generated in wp_localize_script

Take a closer look at the function rest_cookie_check_errors(). When you get the nonce via /wp-json/nonce/v1/get, you’re not sending a nonce in the first place. So this function nullifies your authentication, with this code: if ( null === $nonce ) { // No nonce at all, so act as if it’s an unauthenticated request. wp_set_current_user( 0 … Read more

How to use WP-REST API to login user and get user data for Android app?

I found the simplest solution using the WP-REST API plugin,first set this in yours environment : 1.) In your themes functions.php register API endpoint hooks: add_action( ‘rest_api_init’, ‘register_api_hooks’ ); // API custom endpoints for WP-REST API function register_api_hooks() { register_rest_route( ‘custom-plugin’, ‘/login/’, array( ‘methods’ => ‘POST’, ‘callback’ => ‘login’, ) ); function login() { $output … Read more

wp_get_current_user() function not working in Rest API callback function

Logged in on your website doesn’t mean the user is authenticated in the REST API request, that’s why you are not getting the correct user or a Id = 0 Please take a look to the REST API authentication methods on the docs: https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/ For developers making manual Ajax requests, the nonce will need to … Read more

WP REST API — How to change HTTP Response status code?

You can return a WP_Error object in which you define the status code. Here’s a snippet from the REST API documentation: function my_awesome_func( $data ) { $posts = get_posts( array( ‘author’ => $data[‘id’], ) ); if ( empty( $posts ) ) { return new WP_Error( ‘awesome_no_author’, ‘Invalid author’, array( ‘status’ => 404 ) ); } … Read more

Filtering multiple custom fields with WP REST API 2

This solution works with get_items() in /lib/endpoints/class-wp-rest-posts-controller.php of the v2 WP Rest API. First, you’ll want to construct the GET arguments like you would for a new WP_Query(). The easiest way to do this is with http_build_query(). $args = array ( ‘filter’ => array ( ‘meta_query’ => array ( ‘relation’ => ‘AND’, array ( ‘key’ … Read more