Can we access the REST request parameters from within the permission_callback to enforce a 401 by returning false?

I’ve created a reduced test case that demonstrates that what you want to do is achievable: add_action( ‘rest_api_init’, function() { register_rest_route( ‘wpse/343039’, ‘route’, [ ‘methods’ => [ ‘POST’ ], ‘permission_callback’ => function( WP_REST_Request $request ) { if ( ‘1’ == $request->get_param( ‘param’ ) ) { return true; } else { return false; } }, ‘callback’ … Read more

Adding post fields in wp-json/wp/v2/search

For the search endpoint, the object type (the first parameter for register_rest_field()) is search-result and not the post type (e.g. post, page, etc.). So try with this, which worked for me: add_action( ‘rest_api_init’, function () { // Registers a REST field for the /wp/v2/search endpoint. register_rest_field( ‘search-result’, ‘excerpt’, array( ‘get_callback’ => function ( $post_arr ) … Read more

WP REST API: check if user is logged in

You shouldn’t pass your nonce to your JavaScript to verify it, since client side scripts can be easily manipulated. Instead, you should get the nonce from your front-end content, and then pass it to server to verify it. After verification, you should decide to output content by server, not by your JavaScript file. Something like … Read more

Does jQuery/Ajax send cookies when using the rest API or do I need to somehow add them?

No, you are not passing cookies with jQuery AJAX calls .. certainly not via Cross-domain access. If you’re going to use jQuery to pass data, you need to pass the current user ID and use get_userdata($userid) to determine whether the user has the correct capabilities. Server side: $jQuery_user = get_userdata($_POST[‘user_id’]); if(!user_can($jQuery_user,’publish_posts’)){ return array(‘reply’=>0,’error’=>’Forbidden’,’code’=>’403′); } Client … Read more

WordPress: How to create custom REST API route?

We can create route using rest_api_init action like : Simply add below code to your theme functions.php file. add_action( ‘rest_api_init’, function () { register_rest_route(‘wp/v2’, ‘forgot_password’, array( ‘methods’ => array(‘GET’, ‘POST’), ‘callback’ => ‘forgot_password’ )); } ); function forgot_password(){ // YOUR CALLBACK FUNCTION STUFF HERE } forgot_password will define route URL address. methods will define which … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)