WP-CLI, intuitive commands
The wp cli is a great tool, also it’s commands are intuitive. If we just know the core commands, we can guess what command would be to do something
e.g. what would be a command to create a post? it is, arguments? We can use --prompt flag so the command asks us all the arguments.
It is very easy to learn the commands when we start with an objective, say we have to generate some dummy posts on our demo site, what would be the command for that, it is wp post generate
WP REST
- WP_REST_Server
- WP_REST_Request
- WP_REST_Response
Above classes are used in WP REST API to handle request response
When we register a callback function for our route, object of WP_REST_Request is passed to it, then we can use it to get the body and return some response.
For that we can use the WP_REST_Response class, We can create its object to send custom response , with custom headers and stuff.
The WP_REST_Server is the class used to implement the server
Like most of the things in WordPress, it used an array to store all the endpoints
Endpoint that return `405` on invalid post body
add_action('rest_api_init', 'is_it_json');
function is_it_json() {
register_rest_route('is_it_json/v1', '/verify',array(
'methods' => 'POST',
'callback' => 'is_it_json_callback',
));
}
function is_it_json_callback(WP_REST_Request $request){
$body = $request->get_body();
$data = json_decode($body);
// inValid json
if(null === $data){
return new WP_Error('invalid_json', 'Invalid JSON', array('status' => 405));
}
}
This will only work for the custom route.
WP User and WP Post command
We can perform CRUD operations on Users and posts from this commands
Here’s what I did, first listed all the users on my site, then changed role of one of the user

Leave a Reply