Redirect all 404 to homepage

Home » Snippets » Redirect all 404 to homepage
0

Created with:

Visibility: 

public

Creator: Ahrale

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php

function wpturbo_redirect_404_to_homepage() {
    if (is_404()) {
        wp_redirect(home_url(), 301);
        exit;
    }
}
add_action('template_redirect', 'wpturbo_redirect_404_to_homepage');
				

Explanation:

In this code snippet, we define a function called wpturbo_redirect_404_to_homepage() that checks if the current page is a 404 error page using the is_404() function. If it is, we use the wp_redirect() function to redirect the user to the homepage using the home_url() function. The 301 parameter is used to indicate a permanent redirect. Finally, we exit to stop further execution of the code.

To apply the snippet, simply replace the contents of your 'wp-plugin.php' file with the code snippet provided above. This will ensure that all 404 errors are redirected to the homepage of your WordPress site.

Register an account to save your snippets or go Pro to get more features.