Authentication ensures that only authorized users or systems can access the API, protecting data and functionality from unauthorized use.
Click to reveal answer
intermediate
Name two common authentication methods used in WordPress APIs.
1. Cookie Authentication (used mainly for logged-in users in WordPress). 2. Application Passwords (a simple way to authenticate external apps). 3. OAuth (for more secure, token-based authentication).
Click to reveal answer
intermediate
How does Application Password authentication work in WordPress REST API?
Application Passwords allow external apps to authenticate by sending a username and a special password in the request header. This password is different from the user’s main password and can be revoked anytime.
Click to reveal answer
advanced
What is OAuth and why is it used for API authentication?
OAuth is a secure token-based authentication method that lets users grant limited access to their resources without sharing passwords. It is used to improve security and user control over API access.
Click to reveal answer
beginner
Why should API authentication always use HTTPS?
HTTPS encrypts data sent between client and server, protecting sensitive information like passwords or tokens from being intercepted by attackers.
Click to reveal answer
Which WordPress API authentication method uses a special password different from the user’s main password?
AOAuth
BCookie Authentication
CBasic Authentication
DApplication Passwords
✗ Incorrect
Application Passwords are special passwords created for external apps and are different from the user's main password.
What does OAuth primarily provide for API authentication?
AToken-based access without sharing passwords
BSimple username and password login
CEncryption of data in transit
DAutomatic user registration
✗ Incorrect
OAuth allows users to grant access via tokens without sharing their passwords.
Why is HTTPS important for API authentication?
AIt encrypts data to prevent interception
BIt speeds up API responses
CIt allows anonymous access
DIt stores passwords securely
✗ Incorrect
HTTPS encrypts data sent between client and server, protecting sensitive information.
Which authentication method is mainly used for logged-in WordPress users accessing the REST API?
AApplication Passwords
BOAuth
CCookie Authentication
DAPI Keys
✗ Incorrect
Cookie Authentication uses the logged-in user's session cookies to authenticate API requests.
What is a key benefit of using Application Passwords over regular user passwords?
AThey never expire
BThey can be revoked without changing the main password
CThey allow anonymous API access
DThey are stored in plain text
✗ Incorrect
Application Passwords can be revoked individually without affecting the user's main password.
Explain how authentication protects a WordPress API and describe two common methods used.
Think about who can use the API and how WordPress checks their identity.
You got /3 concepts.
Describe why HTTPS is essential when using authentication for APIs and what risks it helps prevent.
Consider what happens if data is sent without encryption.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of permission_callback in WordPress REST API authentication?
easy
A. To format the API response data
B. To define the URL of the API endpoint
C. To register a new API route
D. To check if the user has permission to access the API endpoint
Solution
Step 1: Understand the role of permission_callback
This callback function is used to verify if the current user has the right permissions to access a specific API route.
Step 2: Differentiate from other API registration parts
Other parts like route URL and response formatting are handled separately; permission_callback specifically controls access.
Final Answer:
To check if the user has permission to access the API endpoint -> Option D
A. The callback function 'get_info' is missing parameters
B. The 'methods' value should be an array, not a string
C. The 'permission_callback' should be a callable, not a function call string
D. The route namespace 'myplugin/v1' is invalid
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 C
Quick Check:
permission_callback must be callable, not called [OK]
Hint: permission_callback needs a callable, not a called function [OK]
Common Mistakes:
Passing function call result instead of callable
Using string with quotes incorrectly
Confusing methods string vs array
5. You want to create a WordPress REST API endpoint that only logged-in users with the 'edit_posts' capability can access. Which of these permission_callback implementations correctly enforces this?
hard
A. permission_callback => function() { return is_user_logged_in(); }
B. permission_callback => function() { return is_user_logged_in() && current_user_can('edit_posts'); }
C. permission_callback => function() { return current_user_can('read'); }
D. permission_callback => function() { return true; }
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 checks current_user_can('read') which many users have but not 'edit_posts'. D allows everyone.