Using WordPress ‘redirect_network_admin_request’ PHP filter

The ‘redirect_network_admin_request’ filter allows you to control whether a request should be redirected to the Network Admin in a WordPress multisite installation.

Usage

add_filter( 'redirect_network_admin_request', 'your_function_name', 10, 1 );

function your_function_name( $redirect_network_admin_request ) {
    // Your code to modify the $redirect_network_admin_request variable
    return $redirect_network_admin_request;
}

Parameters

  • $redirect_network_admin_request (bool): Determines if the request should be redirected to the Network Admin.

Examples

Always redirect to Network Admin

function always_redirect() {
    return true;
}
add_filter( 'redirect_network_admin_request', 'always_redirect' );

In this scenario, we ensure that all requests are always redirected to the Network Admin.

Never redirect to Network Admin

function never_redirect() {
    return false;
}
add_filter( 'redirect_network_admin_request', 'never_redirect' );

This example prevents any requests from being redirected to the Network Admin.

Redirect only for specific users

function redirect_specific_users( $redirect_network_admin_request ) {
    $allowed_users = array( 'user1', 'user2' );
    if ( in_array( wp_get_current_user()->user_login, $allowed_users ) ) {
        return true;
    }
    return $redirect_network_admin_request;
}
add_filter( 'redirect_network_admin_request', 'redirect_specific_users', 10, 1 );

In this example, only specific users (user1 and user2) will be redirected to the Network Admin. Other users will follow the default behavior.

Redirect based on user role

function redirect_by_role( $redirect_network_admin_request ) {
    if ( current_user_can( 'manage_options' ) ) {
        return true;
    }
    return $redirect_network_admin_request;
}
add_filter( 'redirect_network_admin_request', 'redirect_by_role', 10, 1 );

Here, we redirect users with the ‘manage_options’ capability (usually administrators) to the Network Admin. Other users will follow the default behavior.

Redirect based on the day of the week

function redirect_by_day( $redirect_network_admin_request ) {
    $day = date( 'l' );
    if ( $day === 'Monday' ) {
        return true;
    }
    return $redirect_network_admin_request;
}
add_filter( 'redirect_network_admin_request', 'redirect_by_day', 10, 1 );

In this example, we redirect all users to the Network Admin on Mondays. Other days will follow the default behavior.