How do you format the set_body option for WP_Rest_Request?

WP_Rest_Request is not a way to make outgoing calls to remote REST APIs, it’s a data object core uses to pass around information about an incoming request. It’s basically something you recieved, it isn’t something you can send. If you poke your sites REST API with a request, WordPress creates and populates a WP_Rest_Request object … Read more

WP REST API GET Requests require authentication

I’ve been working on a similar issue today. Here’s what I’ve done: add_filter(‘rest_dispatch_request’, function($dispatch_result, $request, $route, $handler) { if (!is_user_logged_in()) { $dispatch_result = new WP_Error( ‘rest_not_logged_in’, __( ‘You are not currently logged in.’ ), array( ‘status’ => 401 ) ); } return $dispatch_result; }, 10, 4); You may want to use the $handler to determine … Read more

I am getting error message on accessing menu-endpoint

You didn’t change your prefix for that plugin’s API. Instead of /MC-ACF/wp-json/wp/v2/menus try /MC-ACF/wp-json/wp-api-menus/v2/menus That plugin developer states it right on the page you referenced in your question. If you’re wondering how I solved this. I’m not sure what your use case is, but there are newer versions of similar plugins as well. That one … Read more

Can you Use the Rest API to query a custom database table

WordPress has no system of assigning privileges or access to control to code that runs in functions.php or in an enabled plugin. Therefore if you can get code to run into functions.php or a plugin in your WordPress environment, you can do anything that you can do in PHP in your web hosting environment, and … Read more

Identical wp_rest nonce returned from rest_api

Despite you already figured it out or found a solution, since this is a follow-up to the other question, I thought it’d be benefecial to post this answer, so hopefully it helps you. =) WordPress uses the native setcookie() function in PHP for setting the authentication cookies, and $_COOKIE for reading the cookies values. But … Read more

permission_callback has no effect

Are you sure that a _doing_it_wrong notice isn’t being issued? You won’t see the notice visibly output on the page because that would break the JSON response. But if you look in the headers of the response you should see a X-WP-DoingItWrong header. It should also appear if you use a plugin like this to … Read more

What is the proper way to validate and sanitize JSON response from REST API?

Thanks to @GTsvetanov from Stackoverflow.com. Missing part of my code is $request->get_json_params(); for getting json request then compare it with schema using rest_validate_value_from_schema() then using rest_sanitize_value_from_schema() for saving proper data to database. $schema = $this->user_playtime_meta_schema(); $val = $request->get_json_params();//<– my mistake $result = rest_validate_value_from_schema( $values, $schema ); if ( ! is_wp_error( rest_validate_value_from_schema( $val, $schema ) ) … Read more

how to send Ajax request in wordpress backend

When you enqueue or localize a script you’re doing it specifically for the front end or the admin. If you want to enqueue or localize a script in both, you have to specifically do it for both. This is used to enqueue/localize for the front end add_action( ‘wp_enqueue_scripts’, ‘your_function_front’ ); your_function_front() { wp_localize_script(‘scripts’, ‘myAjax’, array( … Read more