Skip to content

Instantly share code, notes, and snippets.

@wpflames
Created August 31, 2020 10:25
Show Gist options
  • Select an option

  • Save wpflames/4c04adc82b84e1908f89cf2eb0949784 to your computer and use it in GitHub Desktop.

Select an option

Save wpflames/4c04adc82b84e1908f89cf2eb0949784 to your computer and use it in GitHub Desktop.
Disallow access to WordPress admin area for specific users
//===================================================
// DISALLOW ACCESS TO WPADMIN AREA FOR SPECIFIC USERS
//===================================================
function wps6543_no_admin_access()
{
if (
! is_admin()
|| (
is_user_logged_in()
&& isset( $GLOBALS['pagenow'] ) AND 'wp-login.php' === $GLOBALS['pagenow']
)
) {
return;
}
$redirect = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : home_url( '/' );
if (
current_user_can( 'CAPABILITY_NAME_HERE' )
OR current_user_can( 'CAPABILITY_NAME_HERE' )
)
exit( wp_redirect( $redirect ) );
}
add_action( 'admin_init', 'wps6543_no_admin_access', 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment