Custom Rewrite Rules vs. REST Routes in WordPress
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.
In adherence to our rigorous editorial policy, this article's content has undergone careful testing for accuracy and trustworthiness and hence, this content is marked source of information. View editorial history of this content.
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.
Custom rewrite rules with custom templates
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.
Use custom rewrite rules with custom templates when:
- users need to access your custom URLs from front-end
- you need to serve custom templates based on query variables in URL
Examples:
- User Profile URLs
- Prayer Timing Tool
- Comparison Tool
WordPress REST Routes with Custom Endpoints
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;
}
Use custom REST route with endpoints in WordPress when:
- you just need to access the data from database in your way
- to fetch data for mobile and web app
- to sync data between different apps or servers
Examples:
- App of your WordPress website loading data from your web server
- 3rd party accessing your tool reports or results
- Single Sign On and authorization on the fly
Digital Setups has enforced a strict sourcing policy. Every content piece published on our website is passed through strict editorial review for contextual correctness, communication ethics, and programmatic tests wherever required. Our team research solutions from only credible, authentic, and trustworthy sources. Learn more about our editorial process.
Based on our editorial policy, we update our content time to time to ensure its usefulness, reliability, and validity.
Our standardized editorial process ensures right, timely, and usefulness updates to our content. Your honest opinion drives significant improvement to our content. We appreciate you are taking time to share that.
Readers who read this also found these helpful:
- What you need to know before starting any online business
- Unlock new cross-device capabilities & More with Google Signals
- Does daily manual or dynamically changing content on page impact SEO?
- OPTIONS / RTSP/1.0″ – Apache Access Log Entry
- Apache Access Log Entry: CONNECT Method
- Facebook Meta for Business Suite Hacked. Can you take it back?
- Block Editor: theme.json – Color Palette
- Woo Express vs. Pressable WooCommerce Hosting
- The new and improved Auto ads in AdSense
- Block Editor: Pattern vs. Variation vs. Style vs. Transformation