functions - Redirect User Role When on Specific Page

admin2025-01-07  7

I am trying to redirect user roles to a specific page. If their role is contributor and is trying to access the Users Admin Page they should be redirected. Not sure if I am doing this correctly as it isn't working. Please help

function mwd_redirect_if_on_page() {
   $mwd_current_user = wp_get_current_user();
   if ( user_can( $mwd_current_user, 'contributor')  && is_page('wp-admin/users.php') ) {
      return home_url('specific-page');
   }
}
add_filter( 'login_redirect', 'mwd_redirect_if_on_page' );

I am trying to redirect user roles to a specific page. If their role is contributor and is trying to access the Users Admin Page they should be redirected. Not sure if I am doing this correctly as it isn't working. Please help

function mwd_redirect_if_on_page() {
   $mwd_current_user = wp_get_current_user();
   if ( user_can( $mwd_current_user, 'contributor')  && is_page('wp-admin/users.php') ) {
      return home_url('specific-page');
   }
}
add_filter( 'login_redirect', 'mwd_redirect_if_on_page' );
Share Improve this question asked Mar 17, 2021 at 17:21 Alex MAlex M 1335 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You can achieve your goal this way:

// Block Access to /wp-admin
function mwd_redirect_if_on_page() {
  if ( is_user_logged_in() && is_admin() && current_user_can( 'contributor' ) && (defined( 'DOING_AJAX' ) && !DOING_AJAX) ) ) {
    wp_redirect( home_url('specific-page') );
    exit;
  }
}
add_action( 'init', 'mwd_redirect_if_on_page' );

If you carefully see I've added ajax check. This way contributor won't be able to access wp-admin but still be able to other ajax requests if there any.

That is_page('wp-admin/users.php') is not going to work because is_page() is for posts of the page type only and the function uses the main WP_Query call (which runs on page load) which doesn't happen on the users.php page.

But you can use the global $pagenow variable to check if the current admin page is users.php.

Here's an example using the admin_init hook:

add_action( 'admin_init', 'wpse_385245_1' );
function wpse_385245_1() {
    global $pagenow;
    if ( 'users.php' === $pagenow &&
        in_array( 'contributor', wp_get_current_user()->roles ) ) {
        wp_redirect( home_url( 'specific-page' ) );
        exit;
    }
}

Or you can instead use the load-users.php hook which runs upon loading the users.php page, hence you would no longer need to check the $pagenow value:

add_action( 'load-users.php', 'wpse_385245_2' );
function wpse_385245_2() {
    if ( in_array( 'contributor', wp_get_current_user()->roles ) ) {
        wp_redirect( home_url( 'specific-page' ) );
        exit;
    }
}

However, that hook runs only after the admin_init and other hooks, so if you don't want to "wait" for those extra hooks to complete, then just use admin_init.

Notes:

  • $pagenow is an admin-side-only global variable.

  • I didn't use current_user_can( 'contributor' ) because the documentation says: "While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.". And that also applies to the user_can() function.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262242a803.html

最新回复(0)