Summary: Revising basics of WordPress REST API and WP-CLI
WordPress REST API
The REST API allows us to interact with WordPress programmatically using HTTP requests. It’s useful for creating, reading, updating, and deleting WordPress content from external applications.
- Endpoints: REST API endpoints are URLs that represent specific data and actions. Each endpoint corresponds to a particular resource (e.g., posts, pages, custom post types).
- Routes: Define the URLs for API requests. Routes are associated with specific HTTP methods (GET, POST, PUT, DELETE).
- Controllers: Handle the logic for processing requests to specific routes. They often include methods to handle CRUD operations.
- Namespaces: Used to group routes and avoid conflicts with other plugins or themes. For example,
wp/v2
is a namespace for core WordPress routes.
Common Endpoints
- Posts:
/wp-json/wp/v2/posts
- Pages:
/wp-json/wp/v2/pages
- Custom Post Types:
/wp-json/{namespace}/{post_type}
- Taxonomies:
/wp-json/wp/v2/taxonomies
- Users:
/wp-json/wp/v2/users
Authentication
- Application Passwords: Used for authenticating API requests. Set up in the WordPress admin under user profile settings.
- OAuth: Another method of authentication, often used for more secure and complex integrations.
- Basic Authentication: Not recommended for production due to security risks. Useful for testing.
Common Methods
- GET: Retrieve data from the API. E.g., list posts or get details of a specific post.
- POST: Create new resources. E.g., add a new post.
- PUT/PATCH: Update existing resources. E.g., modify a post.
- DELETE: Remove resources. E.g., delete a post.
Customization
- Custom Endpoints: Create custom routes and controllers for additional functionality. Use
register_rest_route()
to define custom routes. - Response Data: Customize the data returned by endpoints using
register_rest_field()
or by extending REST controllers. - Permissions: We can control access to endpoints by specifying permissions in route definitions. Use
__return_true
for public endpoints or add custom permission callback functions.
function my_custom_route() {
register_rest_route('my_namespace/v1', '/my_endpoint/', array(
'methods' => 'GET',
'callback' => 'my_custom_callback',
));
}
add_action('rest_api_init', 'my_custom_route');
function my_custom_callback($data) {
return new WP_REST_Response('Hello World', 200);
}
WP-CLI
WP-CLI is a command-line interface for managing WordPress installations. It allows us to perform tasks like updating plugins, managing users, and running custom commands from the command line.
Basic Commands
- Core
wp core download
– Download WordPress core files.wp core update
– Update WordPress core.wp core version
– Show the current WordPress version.
- Plugins
wp plugin list
– List installed plugins.wp plugin install <plugin>
– Install a plugin.wp plugin activate <plugin>
– Activate a plugin.wp plugin update <plugin>
– Update a plugin.
- Themes
wp theme list
– List installed themes.wp theme install <theme>
– Install a theme.wp theme activate <theme>
– Activate a theme.
- Users
wp user list
– List users.wp user create <user> <email>
– Create a new user.wp user update <user>
– Update a user’s details.
Custom Commands
- Add Custom Commands: We can extend WP-CLI by creating custom commands. Define a class with methods for the command functionality and use
WP_CLI::add_command()
to register it.
if (defined('WP_CLI') && WP_CLI) {
class My_CLI_Command extends WP_CLI_Command {
public function example($args, $assoc_args) {
WP_CLI::success('This is a custom command');
}
}
WP_CLI::add_command('my_command', 'My_CLI_Command');
}
Leave a Reply