Change WordPress user roles and capabilities Forums How to or FAQ Search Pages not working for custom roles Reply To: Search Pages not working for custom roles

#7126
Vladimir
Keymaster

Try to add code below to the end of your active theme functions.php file:


// block Posts menu for all users except administrator
    function remove_posts_menu() {
      global $menu, $submenu;
      
      $user = wp_get_current_user();
      if ( in_array( 'administrator', $user->roles ) ) {
          return;
      }
      //remove 'All posts' submenu item
      if ( isset( $submenu['edit.php'][5] ) ) {
        unset($submenu['edit.php'][5]);
      }
      // remove Posts top level menu
      if ( isset( $menu[5] ) ) {
        unset($menu[5]);
      }
      
      // redirect in case of try edit.php URL access
      $result = stripos($_SERVER['REQUEST_URI'], 'edit.php');
      if ($result===false) {
          return;
      }
      $result = stripos($_SERVER['REQUEST_URI'], 'post_type=');
      if ($result!==false) {    // allow access to custom post types
          return;
      }
      
      wp_redirect(get_option('siteurl') . '/wp-admin/index.php');
    }
    add_action('admin_head', 'remove_posts_menu', 100);