Last updated on September 14, 2023

Redirect a WordPress Page to Another URL

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Maintain SEO; redirect WordPress pages.

To redirect a WordPress page to another URL, you can use the wp_redirect() function provided by WordPress. This function sends a redirect header to the browser, instructing it to navigate to a different URL.

Here’s an example code snippet that demonstrates how to redirect a WordPress page to another URL:

function wpsnippets_redirect_page() {
    wp_redirect( 'https://example.com/new-page' );
    exit;
}
add_action( 'template_redirect', 'wpsnippets_redirect_page' );

In this example, we define a custom function wpsnippets_redirect_page() that uses the wp_redirect() function to redirect the page to the specified URL (https://example.com/new-page). The exit statement is used to stop the execution of any further code after the redirect.

To make sure the redirect occurs at the appropriate time, we hook the wpsnippets_redirect_page() function to the template_redirect action using the add_action() function. This action is triggered just before WordPress renders the template for the requested page.

You can modify the URL in the wp_redirect() function to redirect to any desired destination. This code snippet can be useful in scenarios where you want to redirect a specific page to a different URL, such as when you have moved a page to a new location or when you want to redirect visitors to an external website.

Examples

Example 1: Redirect a WordPress Page to Another URL using wp_redirect()

This example demonstrates how to redirect a WordPress page to another URL using the wp_redirect() function.

function wpsnippets_redirect_page() {
    wp_redirect( 'https://example.com/new-page', 301 );
    exit;
}
add_action( 'template_redirect', 'wpsnippets_redirect_page' );

The code above creates a custom function wpsnippets_redirect_page() that uses the wp_redirect() function to redirect the current page to the specified URL (https://example.com/new-page). The second parameter 301 indicates a permanent redirect. The add_action() function is used to hook the custom function to the template_redirect action, ensuring that the redirection occurs at the appropriate time.

Example 2: Redirect a WordPress Page to Another URL using wp_safe_redirect()

This example demonstrates an alternative method to redirect a WordPress page to another URL using the wp_safe_redirect() function.

function wpsnippets_redirect_page() {
    wp_safe_redirect( 'https://example.com/new-page', 301 );
    exit;
}
add_action( 'template_redirect', 'wpsnippets_redirect_page' );

Similar to the previous example, this code creates a custom function wpsnippets_redirect_page() that uses the wp_safe_redirect() function to redirect the current page to the specified URL. The second parameter 301 indicates a permanent redirect. The add_action() function is used to hook the custom function to the template_redirect action.

Example 3: Redirect a WordPress Page to Another URL using a Conditional Statement

This example demonstrates how to redirect a WordPress page to another URL based on a conditional statement.

function wpsnippets_redirect_page() {
    if ( is_page( 'old-page' ) ) {
        wp_redirect( 'https://example.com/new-page', 301 );
        exit;
    }
}
add_action( 'template_redirect', 'wpsnippets_redirect_page' );

In this code, the custom function wpsnippets_redirect_page() checks if the current page is the “old-page” using the is_page() function. If the condition is met, the wp_redirect() function is used to redirect the page to the specified URL (https://example.com/new-page). The second parameter 301 indicates a permanent redirect. The add_action() function is used to hook the custom function to the template_redirect action.

Last updated on September 14, 2023. Originally posted on September 18, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *