Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Last active October 30, 2018 14:38
Show Gist options
  • Save nfsarmento/ebf7b105adb168e708d0a6b5cf34b03e to your computer and use it in GitHub Desktop.
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
/** 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