Last active
October 30, 2018 14:38
-
-
Save nfsarmento/ebf7b105adb168e708d0a6b5cf34b03e to your computer and use it in GitHub Desktop.
WordPress, redirect WP admin URLs for all users apart for the ones declare on the users array
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
/** WordPress, redirect WP admin URLs for all users apart for the ones declare on the users array | |
* | |
* https://www.nuno-sarmento..com | |
*/ | |
function ns_redirect_users_by_role() { | |
global $pagenow; | |
if ( ! defined( 'DOING_AJAX' ) ) { | |
/* create an array of user ids, these users wont have this rules applied to their profiles */ | |
$my_user_ids = array( 14, 1, 17, 37); | |
/* check whether the current user id exists in the above array */ | |
if( !in_array( get_current_user_id(), $my_user_ids ) ) { | |
if($pagenow == 'customize.php'){ | |
wp_redirect(admin_url('/index.php', 'http'), 301); | |
exit; | |
} | |
if($pagenow == 'plugins.php'){ | |
wp_redirect(admin_url('/index.php', 'http'), 301); | |
exit; | |
} | |
if($pagenow == 'themes.php'){ | |
wp_redirect(admin_url('/index.php', 'http'), 301); | |
exit; | |
} | |
if($pagenow == 'options-general.php'){ | |
wp_redirect(admin_url('/index.php', 'http'), 301); | |
exit; | |
} | |
$current_url = add_query_arg(NULL, NULL); | |
$parts = explode('/', $current_url); | |
$last = end($parts); | |
$admin_pages = array( | |
'admin.php?page=wc-settings', | |
'index.php?page=simple_history_page', | |
'edit.php?post_type=acf-field-group', | |
'admin.php?page=gf_settings', | |
'edit.php?post_type=tribe_events&page=tribe-common', | |
'admin.php?page=wpengine-common', | |
'users.php?page=users-user-role-editor.php', | |
'admin.php?page=custom-twitter-feeds', | |
'admin.php?page=agilewc', | |
'admin.php?page=wp-viral-quiz', | |
'admin.php?page=maxmegamenu', | |
); | |
if (in_array($last, $admin_pages)){ | |
wp_redirect(admin_url('/index.php', 'http'), 301); | |
exit; | |
} | |
}// end users array | |
} // if DOING_AJAX | |
} // ns_redirect_users_by_role | |
add_action( 'admin_init', 'ns_redirect_users_by_role' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment