• I’m trying to create an endpoint for an admin dashboard widget, but it seems like the API loads before any user information loads. current_user_can($anything) and is_user_logged_in() always return false, even if I’m logged in as an admin.

    Here’s the code in my plugin file:

    add_action( 'rest_api_init', function() {
        register_rest_route('my_plugin', '/list', array(
            'methods' => 'GET',
            'callback' => 'myplug_rest_query',
    		'permission_callback' => function() {
                return current_user_can('edit_dashboard');
            } //Always gives me a 403
        ) );
    } );
    function myplug_rest_query(WP_REST_Request $request) {
    	return current_user_can('edit_dashboard');
    	//Always false, when I remove the permission callback
    }

    The add_action code is copied straight from the documentation?–?any idea why it’s not working?

Viewing 1 replies (of 1 total)
  • Plugin Author Daniel Bachhuber

    (@danielbachhuber)

    it seems like the API loads before any user information loads.

    Incorrect. The current user object is fully populated when the REST API loads.

    The add_action code is copied straight from the documentation – any idea why it’s not working?

    Can you share the request you’re making? And, are you including a nonce in the request? If you don’t include the nonce, then the REST API will treat the request as logged out.

Viewing 1 replies (of 1 total)
  • The topic ‘User info not available for permission_callback?’ is closed to new replies.