Using WordPress ‘redirect_user_admin_request’ PHP filter

The  ‘redirect_user_admin_request’ filter allows you to control if a request should be redirected to the User Admin section in a WordPress Multisite installation.

Usage

To use this filter, you need to add a custom function to your theme’s functions.php file or a custom plugin. Then, hook the function to the redirect_user_admin_request filter using the add_filter() function.

Code Example

function my_custom_redirect_user_admin_request( $redirect_user_admin_request ) {
    // Your custom logic here
    return $redirect_user_admin_request;
}
add_filter( 'redirect_user_admin_request', 'my_custom_redirect_user_admin_request' );

Parameters

  • $redirect_user_admin_request (bool): Indicates whether the request should be redirected to the User Admin section in Multisite.

Examples

Always redirect to User Admin

function always_redirect_to_user_admin( $redirect ) {
    return true; // Always redirect to User Admin
}
add_filter( 'redirect_user_admin_request', 'always_redirect_to_user_admin' );

This code snippet will always redirect the request to the User Admin section in Multisite.

Never redirect to User Admin

function never_redirect_to_user_admin( $redirect ) {
    return false; // Never redirect to User Admin
}
add_filter( 'redirect_user_admin_request', 'never_redirect_to_user_admin' );

This code snippet prevents any request from being redirected to the User Admin section in Multisite.

Redirect to User Admin only for specific users

function redirect_specific_users_to_user_admin( $redirect ) {
    $user = wp_get_current_user();
    // Redirect only for users with the 'editor' role
    if ( in_array( 'editor', (array) $user->roles ) ) {
        return true;
    }
    return false;
}
add_filter( 'redirect_user_admin_request', 'redirect_specific_users_to_user_admin' );

This code snippet redirects requests to the User Admin section only for users with the ‘editor’ role in Multisite.

Redirect based on custom capability

function redirect_based_on_custom_capability( $redirect ) {
    // Redirect only for users with 'manage_custom_capability' capability
    if ( current_user_can( 'manage_custom_capability' ) ) {
        return true;
    }
    return false;
}
add_filter( 'redirect_user_admin_request', 'redirect_based_on_custom_capability' );

This code snippet redirects requests to the User Admin section only for users with the ‘manage_custom_capability’ capability in Multisite.

Redirect based on user ID

function redirect_based_on_user_id( $redirect ) {
    $user = wp_get_current_user();
    // Redirect only for user with ID 5
    if ( $user->ID === 5 ) {
        return true;
    }
    return false;
}
add_filter( 'redirect_user_admin_request', 'redirect_based_on_user_id' );

This code snippet redirects requests to the User Admin section only for the user with an ID of 5 in Multisite.