Skip to content

Instantly share code, notes, and snippets.

@ebinnion
Created May 23, 2014 20:40
Show Gist options
  • Save ebinnion/0bb0f7077bbe85f488c1 to your computer and use it in GitHub Desktop.
Save ebinnion/0bb0f7077bbe85f488c1 to your computer and use it in GitHub Desktop.
Redirect non-admin user in WordPress
<?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;
}
}
@ebinnion
Copy link
Author

Could probably get rid of the wp_login action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment