Authentication for API ensures only trusted users or apps can access your WordPress data safely.
Authentication for API in Wordpress
add_action('rest_api_init', function () { register_rest_route('myplugin/v1', '/data', array( 'methods' => 'GET', 'callback' => 'my_callback_function', 'permission_callback' => function () { return is_user_logged_in(); }, )); });
The permission_callback controls who can access the API endpoint.
Use WordPress functions like is_user_logged_in() or check tokens here.
register_rest_route('myplugin/v1', '/data', array( 'methods' => 'GET', 'callback' => 'my_callback_function', 'permission_callback' => function () { return current_user_can('read'); }, ));
register_rest_route('myplugin/v1', '/public', array( 'methods' => 'GET', 'callback' => 'my_public_callback', 'permission_callback' => '__return_true', ));
add_filter('rest_authentication_errors', function ($result) { if (!empty($result)) { return $result; } if (!is_user_logged_in()) { return new WP_Error('rest_not_logged_in', 'You must be logged in to access this API.', array('status' => 401)); } return true; });
This code creates a REST API endpoint at /wp-json/example/v1/secret that only logged-in users can access. If you visit this URL while logged in, you get a welcome message. If not logged in, access is denied.
<?php add_action('rest_api_init', function () { register_rest_route('example/v1', '/secret', array( 'methods' => 'GET', 'callback' => function () { return ['message' => 'Hello, authenticated user!']; }, 'permission_callback' => function () { return is_user_logged_in(); }, )); });
Always use permission_callback to protect your API routes.
For external apps, consider using OAuth or Application Passwords for safer authentication.
Test your API endpoints with tools like Postman or browser REST clients.
Authentication keeps your WordPress API secure and private.
Use permission_callback to control access to API routes.
Test and choose the right authentication method for your use case.
Practice
permission_callback in WordPress REST API authentication?Solution
Step 1: Understand the role of
This callback function is used to verify if the current user has the right permissions to access a specific API route.permission_callbackStep 2: Differentiate from other API registration parts
Other parts like route URL and response formatting are handled separately;permission_callbackspecifically controls access.Final Answer:
To check if the user has permission to access the API endpoint -> Option DQuick Check:
permission_callback controls access permission [OK]
- Confusing permission_callback with route registration
- Thinking permission_callback formats data
- Assuming permission_callback sets URL
Solution
Step 1: Identify the correct parameter for authentication
The correct parameter to control access ispermission_callback, which should be set to a callable that returns true or false.Step 2: Check the syntax of the registration function
register_rest_route('myplugin/v1', '/data', array('methods' => 'GET', 'callback' => 'get_data', 'permission_callback' => '__return_true')) correctly includespermission_callbackwith a valid callable__return_trueallowing access.Final Answer:
register_rest_route('myplugin/v1', '/data', array('methods' => 'GET', 'callback' => 'get_data', 'permission_callback' => '__return_true')) -> Option AQuick Check:
Use permission_callback for authentication [OK]
- Using 'auth_callback' instead of 'permission_callback'
- Omitting permission_callback entirely
- Using 'permission' instead of 'permission_callback'
register_rest_route('myplugin/v1', '/secret', array(
'methods' => 'GET',
'callback' => 'get_secret_data',
'permission_callback' => function() {
return current_user_can('read');
}
));
function get_secret_data() {
return 'Secret Info';
}Solution
Step 1: Understand the permission_callback logic
The callback checks if the user has the 'read' capability. Unauthenticated users do not have this capability, so it returns false.Step 2: Know WordPress REST API behavior on permission failure
If permission_callback returns false, the API returns a 401 Unauthorized error to block access.Final Answer:
Returns a 401 Unauthorized error -> Option AQuick Check:
Permission denied returns 401 error [OK]
- Assuming data is returned without permission
- Confusing 401 Unauthorized with 404 Not Found
- Thinking empty string is returned on failure
register_rest_route('myplugin/v1', '/info', array(
'methods' => 'GET',
'callback' => 'get_info',
'permission_callback' => 'current_user_can('read')'
));
function get_info() {
return 'Info';
}Solution
Step 1: Check the type of permission_callback
permission_callback must be a callable function, not the result of a function call. Here, 'current_user_can('read')' is a string calling the function immediately, which is incorrect.Step 2: Correct usage of permission_callback
It should be a callable like a function name or anonymous function that returns a boolean when called by the API.Final Answer:
The 'permission_callback' should be a callable, not a function call string -> Option CQuick Check:
permission_callback must be callable, not called [OK]
- Passing function call result instead of callable
- Using string with quotes incorrectly
- Confusing methods string vs array
permission_callback implementations correctly enforces this?Solution
Step 1: Understand the requirement
The endpoint must allow only logged-in users who have the 'edit_posts' capability.Step 2: Analyze each permission_callback
permission_callback => function() { return is_user_logged_in() && current_user_can('edit_posts'); } checks both if the user is logged in and if they have the capability, fulfilling the requirement exactly.Step 3: Why other options fail
A only checks login without the 'edit_posts' capability. B checkscurrent_user_can('read')which many users have but not 'edit_posts'. D allows everyone.Final Answer:
permission_callback => function() { return is_user_logged_in() && current_user_can('edit_posts'); } -> Option BQuick Check:
Check login and capability together [OK]
- Using wrong capability like 'read' instead of 'edit_posts'
- Checking only login without capability
- Allowing all users by returning true
Ready to prove it?
Pass the test and this pattern moves unknown
