WordPress REST API Generator

Generate custom WordPress REST API endpoints

Theme slug
Methods available: GET, POST
Endpoint function name with parameters
REST API function content

AUTO GENERATED CODE

<?php
//Custom REST API endpoint generated by Brewpy.app
add_action('rest_api_init', function () {
 register_rest_route( 'twentytwentytwo/v1', 'latest-users/(?P<user_string>\d+)',array(
               'methods'  => 'GET',
               'callback' => 'brewpy_callback_1642621953930'
     ));
	});
function brewpy_callback_1642621953930(){
$args = array('search' => $request['user_string']); $users = get_users($args);   if (empty($users)) {return new WP_Error( 'empty_users', 'No users to show', array('status' => 404) );}$response = new WP_REST_Response($users);$response->set_status(200);return $response;
}
?>

How to use this code?

  1. Copy auto generated code ↑ (without beginning and ending PHP tags <?php and ?>)
  2. Paste code to bottom of your theme functions.php file
  3. Save and you are done!