search ]

Redirect 404 Errors to Homepage

If you want to redirect users who land on a non-existent page (404) to the homepage, add the following code. Note: Google recommends showing a custom 404 page. For more on what causes 404 errors and how to handle them.

add_action( 'template_redirect', function() {
	if ( is_404() ) {
		wp_redirect( home_url(), 302 );
		exit;
	}
} );
Join the Discussion
0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo