• Resolved mwcs

    (@mwcs)


    Is it possible to allow other roles to see who’s logged in, i.e. Shop Managers and Editors?

    Is there some custom snippet code that I can add?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Ben HartLenn

    (@bhartlenn)

    I am planning on making a settings page, when I get time someday, and among other things it will give users the ability to define roles, potentially capabilities even, then any roles entered would then be able to see the meta box.

    For now, if you really really really want to play around with the plugin code, I will help you: but, please understand that you’re doing this at your own risk. ?? Also be aware that a future update to this plugin would erase the code changes you make: but, hopefully at that point you would be able to easily re add the roles through a settings page for the plugin. It could be a bit of a hiccup to watch out for is all.

    To minimize the risk of your live site going down, I’d recommend you try to do this change on a development/localhost version of your site before pushing the changes to a live site, but that’s up to you. So, if you open up the php file located at: yourwebsitefolder/wp-content/plugins/whos-logged-in/whos-logged-in.php, look at the block of code starting with a comment on line 90. It looks like this currently:

    // if current user is an administrator add meta box during dashboard setup
    add_action( 'wp_dashboard_setup', 'wli_add_metabox' );
    function wli_add_metabox() {
        if ( current_user_can( 'administrator' ) ) {
            add_meta_box( 'wli-metabox', __( 'Who\'s Logged In', 'whos-logged-in' ), 'wli_metabox_ouput', 'dashboard', 'side', 'high' );
        }
    }

    The if statement on line 93 is what you would want to modify by adding another current_user_can() conditional method for each role you want to use(capabilities can be used instead of roles as well). I’ll give you an example for administrators, and editors, and maybe shop managers(maybe because I’m not sure if the role name I use will match yours, but you can check that on your end) to be able to see the metabox.

    // if current user is an administrator, or an editor, or a shop-manager add meta box during dashboard setup
    add_action( 'wp_dashboard_setup', 'wli_add_metabox' );
    function wli_add_metabox() {
        if ( current_user_can( 'administrator' ) || current_user_can( 'editor' ) || current_user_can( 'shop-manager' ) ) {
            add_meta_box( 'wli-metabox', __( 'Who\'s Logged In', 'whos-logged-in' ), 'wli_metabox_ouput', 'dashboard', 'side', 'high' );
        }
    }

    Notice the || in between the three current_user_can conditional methods, that means “OR” in PHP conditional statements. So with the change made the code is now checking to see if a user is an administrator OR if the user is an editor, OR if they are a shop manager(again, check that role name matches the one on your website, I can’t be sure), and if they are any of those roles, then proceed to setup the metabox on their dashboard page.

    Good luck, and let me know if you get stuck, I’m happy to help when I can.

    • This reply was modified 4 years, 9 months ago by Ben HartLenn.
    Plugin Author Ben HartLenn

    (@bhartlenn)

    Marked as resolved, feel free to ask for help still.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to make it visible to more user roles?’ is closed to new replies.