Stage Switcher - Limit user visibility

I just started using Stage Switcher, and it’s great. I have an idea for a feature, and wanted to solicit some feedback before putting work into it. It would be nice if there was a way to only show the dropdown for certain users. I’m using it on a WP based webapp, where all users are logged in, and currently, they all see stage switcher’s options – but don’t need to. On this site it could be role based, but on other sites I may have non-technical admin users who don’t need to see it either.
I can think of a few ways to do this:

  1. Add a checkbox to user profiles, allowing stage switcher to be enabled.
  2. Adding an options page / config options to select who it shows up for.

Thoughts? There are caveats to both of those ways.

That sounds like a good idea but I don’t think we’d include it in the plugin by default. Right now it’s a pretty simple plugin.

You can easily add your own functionality and set the bedrock/stage_switcher_visibility filter which is included for purposes like this one :slight_smile:

Cool beans! Thanks for the input

Hi @swalkinshaw, could you explain to me how bedrock/stage_switcher_visibility filter works? I would like to disable the switcher for non admins users, thank you. :slight_smile:

add_filter('bedrock/stage_switcher_visibility', function($visibility) {
  return !is_admin();
});

It’s a standard filter so something like that should work. By default it’s hidden for non-super admins.

Sorry @swalkinshaw, but this doesn’t work for me and by default, I see the switcher for all users. Maybe could be an issue caused by this, using and (&&) conditional instead of or (||)? Thank you for the support.

Are you using what @swalkinshaw provided or something else? You’ll need to modify his example as is_admin only checks to see whether or not you’re in the WP admin (and not someone who isn’t an administrator).

Hi @ben, sorry for the delay. Sure, I’ve tried these two snippets:

add_filter('bedrock/stage_switcher_visibility', function($visibility) {
    if (!current_user_can('manage_options')) {
        return false;
    }
    return true;
});
add_filter('bedrock/stage_switcher_visibility', function($visibility) {
    global $current_user;
    $role = $current_user->roles[0];
    if ($role === 'subscriber') {
        return false;
    }
    return true;
});

I’ve also tried to invert the return (true and false), but nothing happens, I can’t hide the switcher for non-admin users (or at least for the subscriber role).

I’ve also tried to deactivate all the plugins (except wp-stage-switcher) to prevent conflicts.

Thank you for the support.

Doesn’t seem to work for me either. Even this has no effect to the site:

add_filter('bedrock/stage_switcher_visibility', function ($visible) {
    die;
    return $visible;
}, PHP_INT_MAX);

@swalkinshaw @ben any hint about this issue? Thank you all. :slight_smile:

Nope, we have not looked into it. If this is a bug with the plugin then a PR that fixes the issue would be very appreciated.

Hi @ben, I’ve opened a pull request with a fix for the issue. Thank you!

1 Like