PHP 39
Backend remove menus (extendet) By micha on 27th August 2019 10:07:22 AM
  1. <?php
  2. function remove_menus(){
  3.  
  4.   /*
  5.    * Available defaults
  6.    */
  7.  
  8.   /*
  9.   remove_menu_page( 'index.php' );                  //Dashboard
  10.   remove_menu_page( 'jetpack' );                    //Jetpack*
  11.   remove_menu_page( 'edit.php' );                   //Posts
  12.   remove_menu_page( 'upload.php' );                 //Media
  13.   remove_menu_page( 'edit.php?post_type=page' );    //Pages
  14.   remove_menu_page( 'edit-comments.php' );          //Comments
  15.   remove_menu_page( 'themes.php' );                 //Appearance
  16.   remove_menu_page( 'plugins.php' );                //Plugins
  17.   remove_menu_page( 'users.php' );                  //Users
  18.   remove_menu_page( 'profile.php' );                //Profile
  19.   remove_menu_page( 'tools.php' );                  //Tools
  20.   remove_menu_page( 'options-general.php' );        //Settings
  21.   */
  22.  
  23.   /*
  24.    * Current user can -> https://codex.wordpress.org/Function_Reference/current_user_can
  25.    * Roles and capabilities -> https://codex.wordpress.org/Roles_and_Capabilities
  26.   */
  27.  
  28.  
  29.   /*
  30.    * Role -> Is not Administrator
  31.    */
  32.   if (!(current_user_can('administrator'))) {
  33.     remove_menu_page( 'plugins.php' );                //Plugins
  34.     remove_menu_page( 'profile.php' );                //Profile
  35.    
  36.     /*
  37.      * Role -> KHD Management
  38.      */
  39.     if ( current_user_can('khd_management') ) {
  40.       remove_menu_page( 'options-general.php' );      //Settings
  41.       remove_menu_page( 'themes.php' );               //Appearance
  42.       remove_menu_page( 'wpcf7' );                    //Contact forms
  43.       remove_menu_page( 'tools.php' );                //Tools
  44.       remove_menu_page( 'edit.php' );                 //Posts
  45.       remove_menu_page( 'edit.php?post_type=page' );  //Pages
  46.       remove_menu_page( 'edit-comments.php' );        //Comments
  47.     }
  48.  
  49.     /*
  50.      * Role -> KHD Admin
  51.      */
  52.     if ( current_user_can('khd_admin') ) {
  53.       remove_menu_page( 'tools.php' );                //Tools
  54.     }
  55.  
  56.     /*
  57.      * Role -> default users without a special role from LDAP
  58.      */
  59.     if ( current_user_can('subscriber') ) {
  60.       wp_safe_redirect( home_url() );
  61.     }
  62.    
  63.   }
  64.  
  65. }
  66. add_action( 'admin_menu', 'remove_menus' );
  67.  
  68.  
  69. /*
  70.  * Hard remove Menu items if the hook `admin_menu` is not working at some menus
  71.  */
  72. function remove_menus_admin_init() {
  73.  
  74.   /*
  75.    * Role -> KHD Management
  76.    */
  77.   if ((current_user_can('khd_management'))) {
  78.     remove_menu_page( 'activity_log_page' );
  79.     remove_menu_page( 'khd-general-settings' );
  80.     //wp_redirect( admin_url( 'edit.php?post_type=message' ) );
  81.   }
  82.  
  83.   /*
  84.    * Role -> KHD Admin
  85.    */
  86.   if ((current_user_can('khd_admin'))) {
  87.     remove_menu_page( 'mo_ldap_local_login' );                //LDAP
  88.     remove_menu_page( 'edit.php?post_type=acf-field-group' ); //ACF
  89.     remove_menu_page( 'edit-comments.php?page=wpdiscuz_options_page' );
  90.   }
  91. };
  92. add_action('admin_init', 'remove_menus_admin_init');
  93.  
  94.  
  95. /*
  96.  * DEBUG - IF MENUS WILL NOT HIDE - locate the plugin-slug [02] and use it instead menu-slug in `admin_init`
  97.  */
  98.  
  99. /*
  100. function the_dramatist_debug_admin_menu() {
  101.  
  102.   echo '<pre>' . print_r( $GLOBALS[ 'menu' ], TRUE) . '</pre>';
  103. }
  104. add_action( 'admin_init', 'the_dramatist_debug_admin_menu' );
  105. */
  106.  
  107. function remove_dashboard_widgets() {
  108.   global $wp_meta_boxes;
  109.   //print_r($wp_meta_boxes);
  110.   /*
  111.    * Role -> KHD Management
  112.    */
  113.   if ((current_user_can('khd_management'))) {
  114.     unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
  115.     //unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
  116.     unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
  117.     //unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']); // WP Infos (Posts, Pages, ...)
  118.     unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']); // Acivity Log
  119.     unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
  120.     unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
  121.     unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
  122.     unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
  123.     unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
  124.   }
  125.   /*
  126.    * Role -> KHD Admin
  127.    */
  128.   if ((current_user_can('khd_admin'))) {
  129.     unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
  130.     unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
  131.   }
  132. }
  133. add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );
  134.  
  135.  
  136. /*
  137.  * Admin Top bar
  138.  */
  139. function remove_wp_nodes() {
  140.   global $wp_admin_bar;
  141.   $wp_admin_bar->remove_node( 'wp-logo' );
  142.   $wp_admin_bar->remove_node( 'new-content' );  //New page, post, media, link, etc.
  143.   $wp_admin_bar->remove_node( 'archive' );      //View Post type archive link
  144.   $wp_admin_bar->remove_node( 'my-account' );   //Top Right Account info
  145. }
  146. add_action( 'admin_bar_menu', 'remove_wp_nodes', 999 );
  147.  
  148.  
  149. /*
  150.  * Rename Posts to News Articles
  151.  */
  152. function change_post_menu_label() {
  153.   global $menu;
  154.   global $submenu;
  155.   $menu[5][0] = 'News Articles';
  156.   $submenu['edit.php'][5][0] = __('News Articles', LOCAL);
  157.   $submenu['edit.php'][10][0] = __('Add News Article', LOCAL);
  158.   $submenu['edit.php'][16][0] = __('Tags', LOCAL);
  159.   echo '';
  160. }
  161. add_action( 'admin_menu', 'change_post_menu_label' );
  162.  
  163. function change_post_object_label() {
  164.   global $wp_post_types;
  165.   $labels = &$wp_post_types['post']->labels;
  166.   $labels->name = __('News Articles', LOCAL);
  167.   $labels->singular_name = __('News Article', LOCAL);
  168.   $labels->add_new = __('Add News Article', LOCAL);
  169.   $labels->add_new_item = __('Add News Article', LOCAL);
  170.   $labels->edit_item = __('Edit News Article', LOCAL);
  171.   $labels->new_item = __('News Article', LOCAL);
  172.   $labels->view_item = __('View News Article', LOCAL);
  173.   $labels->search_items = __('Search News Articles', LOCAL);
  174.   $labels->not_found = __('No News Articles found', LOCAL);
  175.   $labels->not_found_in_trash = __('No News Articles found in Trash', LOCAL);
  176. }
  177. add_action( 'init', 'change_post_object_label' );
  178.  
  179.  
  180. /*
  181.  * Update Notes
  182.  */
  183. function hide_update_notice_to_all_but_admin_users()
  184. {
  185.   if (!current_user_can('update_core')) {
  186.     remove_action( 'admin_notices', 'update_nag', 3 );
  187.   }
  188. }
  189. add_action( 'admin_head', 'hide_update_notice_to_all_but_admin_users', 1 );

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.