Just revamped. Got feedback? Share now.

Privacy
Subscribe

Custom Rewrite Rules vs. REST Routes in WordPress

By Digital Setups Published 12 Mar, 2023 Modified 12 Mar, 2023 Viewed 56 times Read in 2m 53s
Experience Based
Content Validity: Always Valid or Evergreen Comprehension: Level 2 – For Professionals

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.

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

×

Your feedback matters.

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.

×