Skip to content

Instantly share code, notes, and snippets.

@aristath
Last active December 17, 2015 06:38
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 aristath/5566730 to your computer and use it in GitHub Desktop.
Save aristath/5566730 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Custom Limit Admin Access
Plugin URI: http://shoestrap.org
Description: Limits access to the dashboard
Author: Aristeides Stathopoulos
Version: 1.0
Author URI: http://aristeides.com
*/
/*
* Removes some basic menu items
*/
function aristath_custom_admin_theme_remove_menus () {
if( !current_user_can( 'manage_network' ) ) {
global $menu;
$restricted = array(__('Posts'), __('Media'), __('Links'), __('Pages'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'), __('QRcode'), __('CustomPress'));
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}
// Remove some extra menu items
remove_menu_page( 'edit-comments.php' );
remove_menu_page( 'themes.php' );
remove_menu_page( 'plugins.php' );
remove_menu_page( 'admin.php?page=mp_st' );
remove_menu_page( 'admin.php?page=cp_main' );
remove_submenu_page( 'edit.php?post_type=product', 'edit-tags.php?taxonomy=product_category&amp;post_type=product' );
remove_submenu_page( 'edit.php?post_type=product', 'edit-tags.php?taxonomy=brand&amp;post_type=product' );
remove_submenu_page( 'edit.php?post_type=product', 'edit-tags.php?taxonomy=model&amp;post_type=product' );
remove_submenu_page( 'edit.php?post_type=product', 'edit-tags.php?taxonomy=product_tag&amp;post_type=product' );
}
}
add_action('admin_menu', 'aristath_custom_admin_theme_remove_menus', 10);
/*
* Removes some dashboard widgets
*/
function aristath_custom_remove_dashboard_widgets() {
remove_meta_box('icl_dashboard_widget', 'dashboard', 'normal');
}
add_action('admin_init', 'aristath_custom_remove_dashboard_widgets');
/*
* Adds a stylesheet to the dashboard
*/
function aristath_custom_extras_admin_head() {
echo '<link rel="stylesheet" type="text/css" href="' .plugins_url('admin.css', __FILE__). '">';
}
add_action('admin_head', 'aristath_custom_extras_admin_head');
/*
* Hides a menu item when on Pro-Sites the site's level is 1
*/
function aristath_custom_hide_domain_menu_when_not_needed() {
global $wpdb;
$blog_id = $wpdb->blogid;
$sql = "SELECT level FROM {$wpdb->base_prefix}pro_sites WHERE blog_ID = '$blog_id'";
$level = $wpdb->get_var($sql);
if ($level == 1){
echo '<style>li#toplevel_page_tools-page-domainmapping{display: none !important;}</style>';
}
}
add_action('admin_head', 'aristath_custom_hide_domain_menu_when_not_needed');
/*
* If the user is not an admin, deny access to the backend and redirect to the homepage.
*/
function aristath_custom_no_admin_access(){
if( !current_user_can( 'administrator' ) ) {
wp_redirect( home_url() );
die();
}
}
add_action( 'admin_init', 'aristath_custom_no_admin_access', 1 );
/*
* Hide the admin bar
*/
function basic_function_admin_bar(){
return false;
}
add_filter( 'show_admin_bar' , 'basic_function_admin_bar');
/*
* Restrict access to some areas of the site.
* If someone without the proper permissions tries to access them,
* redirect them to the site's homepage.
*/
function aristath_custom_restrict_admin_with_redirect() {
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/widgets.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/user-new.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/upgrade-functions.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/upgrade.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/themes.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/theme-install.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/theme-editor.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/setup-config.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/plugins.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/plugin-install.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/options-writing.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/options-reading.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/options-privacy.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/options-permalink.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/options-media.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/options-head.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/options-general.php.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/options-discussion.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/options.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/network.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/ms-users.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/ms-upgrade-network.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/ms-themes.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/ms-sites.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/ms-options.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/ms-edit.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/ms-delete-site.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/ms-admin.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/moderation.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/menu-header.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/menu.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/edit-tags.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/edit-tag-form.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/edit-link-form.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/edit-comments.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/credits.php'){ wp_redirect(admin_url() ); exit; }
if (!current_user_can('manage_network') && $_SERVER['PHP_SELF'] == '/wp-admin/about.php'){ wp_redirect(admin_url() ); exit; }
}
add_action('admin_init', 'aristath_custom_restrict_admin_with_redirect');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment