How to redirect home page to any URL in WordPress using the PHP redirect

Sometimes you may want to redirect your WordPress home page to a different URL — maybe to a landing page, an external site, or a promotional campaign page. Instead of using plugins or .htaccess rules, you can achieve this efficiently with a simple PHP redirect inside your theme or custom plugin.

Below is a straightforward method using WordPress’s native wp_redirect() function, ensuring a clean and reliable redirect from your home page to any URL you choose.

Step-by-step PHP Redirect of Home Page in WordPress

Add this code to your theme’s functions.php file or in a site-specific plugin:

function redirect_homepage_to_custom_url() {
// Check if current page is the homepage
if ( is_front_page() || is_home() ) {
// URL to redirect to
$redirect_url = 'https://example.com/your-target-page';

// Perform the redirect
wp_redirect( $redirect_url );
exit; // Always call exit after wp_redirect
}
}
add_action( 'template_redirect', 'redirect_homepage_to_custom_url' );

Explanation:

  • is_front_page() checks if the current page is the site’s front page (static homepage).

  • is_home() checks if the current page is the blog posts index (for sites using posts as homepage).

  • wp_redirect() sends an HTTP redirect header to the browser.

  • exit; stops execution after the redirect to prevent further output.

  • The function hooks into template_redirect, which fires before WordPress renders the page content.

Important Notes:

  • Use wp_redirect() instead of PHP’s native header() function because it integrates better with WordPress internals.

  • Ensure no output (like spaces or echoes) occurs before the redirect, or it will cause header errors.

  • Replace 'https://example.com/your-target-page' with the actual URL you want visitors redirected to.

FAQs

Can I redirect the WordPress home page using PHP code?
Yes, by adding a function hooked to template_redirect that checks if the current page is the home page, you can use wp_redirect() to send visitors to any URL you want.

Where should I put the redirect code?
The code should go into your active theme’s functions.php file or a custom site-specific plugin to ensure it runs properly.

What’s the difference between is_front_page() and is_home()?
is_front_page() checks if the current page is the site’s static front page (set in Reading Settings), while is_home() checks if it’s the blog posts index page. Both are checked to cover different homepage setups.

Why do I need to call exit; after wp_redirect()?
Calling exit; stops PHP execution immediately after sending the redirect headers, preventing WordPress from continuing to load and output content.

Can I use header('Location: ...') instead of wp_redirect()?
It’s possible but not recommended, as wp_redirect() handles WordPress-specific tasks and avoids conflicts with plugins or themes.

What happens if there is output before the redirect code runs?
Any output before redirect headers can cause “headers already sent” errors, making the redirect fail. Make sure no echoes, print statements, or whitespace are sent before calling wp_redirect().