Why Create A Rest API?
API’s are used to exchange data between remote insides or to collect data internally from websites. It’s a standardized way of sending and receiving information. JSON encoding is used as the standardized format for receiving data.
Instead of including remote files, or getting a remote file with a function like file_get_contents() and dealing with raw HTML, API’s provide a way to standardize data transfer.
Luckily, WordPress’s built in api is pretty good!
Full Rest API Endpoint Example
/**
* Scottsweb.dev - create a rest api endpoint
* https://scottsweb.dev/how-to-create-rest-api-endpoint-in-wordpress/
*/
function create_rest_endpoint() {
register_rest_route('users', 'get_user', array(
'method' => 'get',
'callback' => 'do_get_user',
'permission_callback' => 'do_get_permissions'
));
}
function do_get_permissions() {
return true;
}
function do_get_user() {
$result = array(
'name' => 'Scott',
'website' => 'scottsweb.dev'
);
$result['success'] = true;
wp_send_json($result, 200);
}
add_action('rest_api_init', 'create_rest_endpoint');
Put this code in a globally accessible space, like your child themes functions.php.
Now, let’s break it down!
Attach A Callback Function To rest_api_init
add_action('rest_api_init', 'create_rest_endpoint');
This line tells WordPress to run function ‘create_rest_endpoint’ when the rest api is being initialized.
Creating The Callback Function
function create_rest_endpoint() {
register_rest_route('users', 'get_user', array(
'method' => 'get',
'callback' => 'do_get_user',
'permission_callback' => 'do_get_permissions'
));
}
This function is the callback and it registers the rest route. It’s important to note here that I chose “users”. This is a great namespace if it’s a users API. If it were a products API, we would call this “products”. You can create multiple methods in the same namespace. So you could have users/get_user, users/delete_user, users/update_user, etc.
The method here is “get”. It could also be “post” or any other HTTP method like put, update, options, patch, or delete. You can have multiple methods by putting them into an array like array(‘get’, ‘post’);
permissions_callback is optional and if you leave it out, the API endpoint will be public.
Setting API Endpoint Permissions
function do_get_permissions() {
return true;
}
If your API endpoint is meant to be public, this is great. Otherwise you’ll want to perform additional logic here to return true or false based on if the user is authenticated. You could check an API key, or check if cookies are set, or any custom coding you want.
Creating The Method Callback
function do_get_user() {
$result = array(
'name' => 'Scott',
'website' => 'scottsweb.dev'
);
$result['success'] = true;
wp_send_json($result, 200);
}
Here, I just create an array result and return it with wp_send_json. This is where you would check a passed parameter and pull data from a database, usually.
wp_send_json sends the result and you could also use wp_send_json_success or wp_send_json_error. These latter two functions send a success state of true or false with the data, instead of doing it manually.