Change WordPress user roles and capabilities Forums Give user access to plugin – how to How to display admin bar for some user role

Viewing 15 posts - 1 through 15 (of 35 total)
  • Author
    Posts
  • #1603
    bikostudio
    Participant

    Hi,
    I have created a new user role to give it some permission to do some tasks of admin. I’m using another plugin to hide admin bar for logedin users.

    how to display the admin bar for some user role?
    thanks

    #1604
    Vladimir
    Keymaster

    Hi,

    This code shows admin top menu bar for the role:

    add_action('wp_head', 'show_top_admin_menu_bar', 100);
    
    function show_top_admin_menu_bar() {
      if (current_user_can('some_role')) {
          show_admin_bar(true);
      }
    }
    

    Pay attention for the 100 value of a priority parameter. With this value code should be executed after that other plugin did its work.

    You may add it to the functions.php file of your active theme.

    #1606
    bikostudio
    Participant

    Hi Vladimir,

    I did put the code in functions.php file of my active theme, but nothing happened. I have put the user role in if (current_user_can(‘some_role’)) but nothing happened. can you help me please?

    #1607
    bikostudio
    Participant

    I have also disabled the plugin which hide the admin bar

    #1608
    Vladimir
    Keymaster

    Show the code after modification.

    #1609
    bikostudio
    Participant

    I copied the functions.php to the child-theme folder then i pasted in it this code and changed the some_role to the role name at it is in user role editor plugin which is data_entry to be like this :

    add_action(‘wp_head’, ‘show_top_admin_menu_bar’, 100);

    function show_top_admin_menu_bar() {
    if (current_user_can(‘data_entry’)) {
    show_admin_bar(true);
    }
    }

    #1610
    Vladimir
    Keymaster

    Thanks. It seems to be correct.
    Please check, if your child theme is really active.
    Do you use WooCommerce plugin?

    #1611
    bikostudio
    Participant

    Yes I use WooCommerce and I discovered that it coz the problem 🙂
    .. it has function or something blocks it. Ho can i fix this please?

    #1612
    Vladimir
    Keymaster
    #1613
    bikostudio
    Participant

    Thanks i will give it try and tell you 🙂

    #1614
    bikostudio
    Participant

    I did what post said but no change .. I pasted this code to the function.php in child theme folder and the child theme is activated.

    and data entry users can not access admin bar
    code :

    <?php

    add_filter(‘woocommerce_disable_admin_bar’, ‘_wc_disable_admin_bar’, 10, 1);

    function _wc_disable_admin_bar($prevent_admin_access) {
    if (!current_user_can(‘data_entry’)) {
    return $prevent_admin_access;
    }
    return ‘no’;
    }

    add_filter(‘woocommerce_prevent_admin_access’, ‘_wc_prevent_admin_access’, 10, 1);

    function _wc_prevent_admin_access($prevent_admin_access) {
    if (!current_user_can(‘data_entry’)) {
    return $prevent_admin_access;
    }
    return false;
    }

    /* Start writing your functions here

    #1615
    Vladimir
    Keymaster

    Try another variant. Create .php file, open it in editor, start from <?php tag and insert there a code above, save it. Then put that file to the ‘wp-content/mu-plugins/’ folder (“must use” plugins). Will it help?

    #1616
    bikostudio
    Participant

    and what should i name the file?

    #1617
    bikostudio
    Participant

    I don’t see mu-plugins folder too

    #1618
    Vladimir
    Keymaster

    Create ‘mu-plugins’ folder. File name does not matter. WordPress executes any .php file it find in this folder.
    https://codex.wordpress.org/Must_Use_Plugins

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