Created
May 23, 2014 20:40
-
-
Save ebinnion/0bb0f7077bbe85f488c1 to your computer and use it in GitHub Desktop.
Redirect non-admin user in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'wp_login', 'redirect_non_admins', 10, 2 ); | |
function redirect_non_admins( $user_id, $user ) { | |
$user = wp_get_current_user(); | |
$admin_roles = array('administrator'); | |
if( 0 == count( array_intersect( $admin_roles, $user->roles ) ) ) { | |
wp_redirect( home_url() ); | |
exit; | |
} | |
} | |
add_action( 'admin_init', 'non_admin_dash' ); | |
function non_admin_dash() { | |
$user = wp_get_current_user(); | |
$screen = get_current_screen(); | |
$admin_roles = array('administrator'); | |
$is_wp_migrate = false; | |
if( ! isset( $_POST['action'] ) && false === strpos( $_SERVER['REQUEST_URI'], 'profile' ) && 0 == count( array_intersect( $admin_roles, $user->roles ) ) ) { | |
wp_redirect( home_url() ); | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could probably get rid of the wp_login action.