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

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #7111
    edering
    Participant

    Hi. I can’t figure out why Users with any of my Roles that go to Pages see the “Search Pages” function, but when they do a search, it takes them to the Dashboard. Any ideas?

    #7112
    Vladimir
    Keymaster

    Hi,

    Do you use “Admin menu access” add-on with “Block not selected” option? If Yes, read carefully the “Technical details” part of the documentation article.

    #7113
    edering
    Participant

    No, I am using Admin Menu Access with Block Selected.

    #7116
    Vladimir
    Keymaster

    I assume a conflict with some plugin. If it’s possible to look at this on your site send admin credentials and testing role name to support [at-sign] role-editor.com

    #7117
    edering
    Participant

    Thanks, I just sent the email

    #7119
    Vladimir
    Keymaster

    Thanks you.

    The source of the issue is that admin menu item ‘All Posts’ (edit.php) is prohibited (selected) for the role, but allowed are ‘All Pages’ (edit.php?post_type=page).
    When user try to search some page, URL becomes edit.php?post_type=page&s=something…
    which is not apparently allowed and edit.php is in the list of prohibited URLs. Thus URE fulfills the redirection. The same is similar for other custom post types, URL for which starts from edit.php too.

    Quick workaround is allow for a role ‘All Posts’ menu and submenu items. I tested this for IU administrator role and left as it is for your review.

    Let me know if this workaround is suitable for you. If it is not, I may offer you a piece of code which will hide ‘All Posts’ menu items from admin menu directly after adding to the active theme functions.php file.

    #7120
    edering
    Participant

    Thanks. Yes, we don’t use “Posts” so we were hiding it so the Users would not get confused and to keep the WordPress menu clean. How can I hide “Posts” from the menu but keep the function for searching Pages?

    #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);
    
    #7128
    edering
    Participant

    Thanks, that worked!

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.