WordPress.org

Ready to get started?Download WordPress

Forums

Toolbar Roles (3 posts)

  1. olehellhound
    Member
    Posted 1 year ago #

    Hello,

    I'm creating a plugin and I got the idea to create menus in the WP Toolbar for my backend pages. I've added a user as a subscriber and want for the menu that I have created to appear in the subscribers' admin bar because there will be dynamic content created within my plugin. Is this possible, and if so, how do I do it? Here is my code:

    //admin bar objects
    add_action( 'admin_bar_menu', 'toolbar_link_to_mypage', 999 );
    function toolbar_link_to_mypage( $wp_admin_bar ) {
      $args = array(
        'id' => 'Spectrum',
        'title' => 'Manage Spectrum',
        'href' => site_url('/spectrum'),
        'meta' => array('class' => 'Spectrum-page')
      );
      $wp_admin_bar->add_node($args);
    }

    Thanks

  2. bcworkz
    Member
    Posted 1 year ago #

    You have the basic idea for adding menus, assuming you have a page called spectrum, otherwise the href value may need to be to an actual php page.

    To have the node only be added for subscribers, contain the $args and add_node() code in a conditional if() block that checks if the current user has or does not have a particular capability with current_user_can(). Since subscribers have few or no unique capabilities, it's probably best to check for one they do not have.

    There's also a remove_node() method you could add to the conditional if you want to remove other menu items for subscribers only.

  3. olehellhound
    Member
    Posted 1 year ago #

    User error, I was programming way too late last night. My above posted code correctly adds a menu item to the admin bar for subscribers.

    I was developing in my dev server and testing the feature on my production server instead of my dev server for some reason. That's what 3am programming does to you.

    Thanks for your help, the current_user_can() function will allow me to use the roles that I will be creating for determining if a certain "subscriber", or field rep, has access to certain forms or not.

Topic Closed

This topic has been closed to new replies.

About this Topic