maybe_redirect_404() WordPress Function

If a user tries to access a URL on your WordPress site that doesn't exist, they will see a 404 "Not Found" error page. The maybe_redirect_404() function allows you to redirect these users to a different URL, such as your home page, instead of the default 404 page. This can be useful if you want to make sure that users don't end up on a dead-end page on your site. It can also help reduce the number of 404 errors that you see in your website's analytics.

maybe_redirect_404() #

Corrects 404 redirects when NOBLOGREDIRECT is defined.


Source

File: wp-includes/ms-functions.php

function maybe_redirect_404() {
	if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) ) {
		/**
		 * Filters the redirect URL for 404s on the main site.
		 *
		 * The filter is only evaluated if the NOBLOGREDIRECT constant is defined.
		 *
		 * @since 3.0.0
		 *
		 * @param string $no_blog_redirect The redirect URL defined in NOBLOGREDIRECT.
		 */
		$destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT );

		if ( $destination ) {
			if ( '%siteurl%' === $destination ) {
				$destination = network_home_url();
			}

			wp_redirect( $destination );
			exit;
		}
	}
}


Top ↑

Changelog

Changelog
VersionDescription
MU (3.0.0)Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More