Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Last active April 17, 2024 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfsarmento/51538628f924f72b23995d9946065c76 to your computer and use it in GitHub Desktop.
Save nfsarmento/51538628f924f72b23995d9946065c76 to your computer and use it in GitHub Desktop.
Redirect all users that are not in the users array to the wp-admin dashboard page if trying to access the pages below declared
/**
*
* Redirect all users that are not in the users array to the wp-admin dashboard page if trying to access the pages below declared
*
*/
if ( ! function_exists( 'ns_redirect_users_by_role' ) ) :
function ns_redirect_users_by_role() {
global $pagenow;
if ( ! defined( 'DOING_AJAX' ) ) {
/* create an array of user ids, these users wont have the rules below applied to their profiles */
$my_user_ids = array( 1 );
/* 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 == 'plugin-install.php'){
wp_redirect(admin_url('/index.php', 'http'), 301);
exit;
}
if($pagenow == 'export.php'){
wp_redirect(admin_url('/index.php', 'http'), 301);
exit;
}
if($pagenow == 'import.php'){
wp_redirect(admin_url('/index.php', 'http'), 301);
exit;
}
if($pagenow == 'users.php'){
wp_redirect(admin_url('/index.php', 'http'), 301);
exit;
}
if($pagenow == 'user-new.php'){
wp_redirect(admin_url('/index.php', 'http'), 301);
exit;
}
if($pagenow == 'theme-install.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;
}
if($pagenow == 'tools.php'){
wp_redirect(admin_url('/index.php', 'http'), 301);
exit;
}
if($pagenow == 'edit-comments.php'){
wp_redirect(admin_url('/index.php', 'http'), 301);
exit;
}
if($pagenow == 'export-personal-data.php'){
wp_redirect(admin_url('/index.php', 'http'), 301);
exit;
}
if($pagenow == 'erase-personal-data.php'){
wp_redirect(admin_url('/index.php', 'http'), 301);
exit;
}
if($pagenow == 'options.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(
'index.php?page=simple_history_page',
//'edit.php?post_type=acf-field-group',
'admin.php?page=gf_settings',
'admin.php?page=gf_edit_forms',
'admin.php?page=vc-general',
'user-edit.php?user_id=1',
'admin.php?page=wp-mail-smtp',
'admin.php?page=wc-settings',
'admin.php?page=wc-admin&path=%2Fcustomers',
'admin.php?page=wp_stream_settings',
);
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' );
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment