Using WordPress ‘ms_site_not_found’ PHP action

The ms_site_not_found WordPress PHP action fires when a network can be determined but a site cannot. At the time of this action, the only recourse is to redirect somewhere and exit. If you want to declare a particular site, do so earlier.

Usage

add_action('ms_site_not_found', 'your_custom_function', 10, 3);

function your_custom_function($current_site, $domain, $path) {
    // Your custom code here
}

Parameters

  • $current_site (WP_Network): The network that had been determined.
  • $domain (string): The domain used to search for a site.
  • $path (string): The path used to search for a site.

More information

See WordPress Developer Resources: ms_site_not_found

Examples

Redirect to a custom 404 page

Redirect users to a custom 404 page when a site is not found.

add_action('ms_site_not_found', 'redirect_to_custom_404', 10, 3);

function redirect_to_custom_404($current_site, $domain, $path) {
    wp_redirect('/custom-404-page/');
    exit;
}

Redirect to the main site

Redirect users to the main site when a site is not found.

add_action('ms_site_not_found', 'redirect_to_main_site', 10, 3);

function redirect_to_main_site($current_site, $domain, $path) {
    wp_redirect($current_site->domain);
    exit;
}

Log site not found errors

Log site not found errors for further investigation.

add_action('ms_site_not_found', 'log_site_not_found', 10, 3);

function log_site_not_found($current_site, $domain, $path) {
    error_log("Site not found: domain - {$domain}, path - {$path}");
}

Redirect to a search page

Redirect users to a search page with the domain as the search query.

add_action('ms_site_not_found', 'redirect_to_search', 10, 3);

function redirect_to_search($current_site, $domain, $path) {
    wp_redirect('/search/?q=' . urlencode($domain));
    exit;
}

Display a custom error message

Display a custom error message when a site is not found.

add_action('ms_site_not_found', 'display_custom_error_message', 10, 3);

function display_custom_error_message($current_site, $domain, $path) {
    wp_die('The site you are looking for could not be found.', 'Site Not Found', array('response' => 404));
}