Just revamped. Got feedback? Share now.
Understand the difference between Wordpress's custom rewrite rules and custom REST routes. No more confusion when to use rewrite rule or register a route in any WP website, web app, or mobile app.
WordPress provides developers with different ways to modify the behavior of the platform, and two popular methods are WordPress rewrite rules and WordPress REST routes. While these two concepts may seem similar, they have some significant differences.
In this article, we will explore the difference between WordPress rewrite rules and WordPress REST routes and provide examples of when to use each one.
WordPress rewrite rules allow developers to modify the way WordPress handles URLs. When a user visits a WordPress website, the platform receives a URL request and parses it to determine which content to display. WordPress rewrite rules allow developers to customize this process by creating custom URL patterns that map to specific content.
For example, let’s say we have a website with a custom post type called “books
.” By default, the URL for a single book post would look like this:
https://example.com/books/my-book-title/
However, we might want to change the URL structure to include the book’s author and genre, like this:
https://example.com/books/fiction/john-doe/my-book-title/
We can use WordPress rewrite rules to achieve this.
Here’s an example:
function custom_rewrite_rules() {
add_rewrite_rule(
'^books/([^/]+)/([^/]+)/([^/]+)/?$',
'index.php?post_type=books&genre=$matches[1]&author=$matches[2]&name=$matches[3]',
'top'
);
}
add_action( 'init', 'custom_rewrite_rules' );
This code adds a custom rewrite rule that maps the URL structure /books/fiction/john-doe/my-book-title/
to a query for a book post with the post type “books,” the genre “fiction,” the author “john-doe,” and the post name "my-book-title."
WordPress rewrite rules can be powerful, allowing developers to create custom URL structures that make their websites more user-friendly and optimized for search engines. However, they can also be complex and require careful consideration of their impact on the website’s performance.
Examples:
WordPress REST routes allow developers to create custom endpoints for the WordPress REST API. The REST API allows external applications to interact with a WordPress website’s content and data, making it a powerful tool for building custom applications and integrations.
For example, let’s say we have a website with a custom post type called “movies.” We might want to create a custom REST endpoint that returns all movies released in a specific year.
Here’s an example of how we can create this endpoint using WordPress REST routes:
function custom_movie_routes() {
register_rest_route(
'custom/v1',
'/movies/year/(?P<year>\d+)',
array(
'methods' => 'GET',
'callback' => 'custom_get_movies_by_year',
'args' => array(
'year' => array(
'required' => true,
'validate_callback' => function($param, $request, $key) {
return is_numeric($param);
}
)
)
)
);
}
add_action( 'rest_api_init', 'custom_movie_routes' );
function custom_get_movies_by_year($request) {
$year = $request->get_param('year');
$query = new WP_Query(array(
'post_type' => 'movies',
'date_query' => array(
array(
'year' => $year,
),
),
));
return $query->posts;
}
Examples:
Our standardized editorial process ensures right, timely, and healthy updates to our content. Your honest opinion drives significant improvement to our content. We appreciate you are taking time to share that.
100K+ readers have already joined the lists of their choice. Will you? Just now, maybe?
Pick List(s) to Subscribe