Skip Navigation

[Resolved] Hide a button in view for certain roles

This support ticket is created 3 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1931959
Skjermbilde 2021-02-03 kl. 17.59.48.png

Hi,
I wonder if it is possible to hide a button inside a view for spesifique roles.

This is the button I would like to hide for the role: "arbeidstaker"

<button class="btn btn-rediger btn-block" type="button" data-toggle="collapse" data-target="#collapseExample[wpv-post-id]" aria-expanded="false" aria-controls="collapseExample[wpv-post-id]">
Rediger kontrollpunkt
</button>

Unfortunately the entire site is restricted at the moment, and I need to keep it restricted until saturday. It will be demoed tomorrow for customer.

I hope that you despite this are able to provide a code to hide the button for spesifique roles. I have tested conditional code, but then the entire view goes away.

hidden link

#1932413
Screen Shot 2021-02-03 at 3.35.41 PM.png
Screen Shot 2021-02-03 at 3.35.02 PM.png

Hello, there are a couple of different ways you can restrict access based on User roles. If your site uses the Toolset Access plugin, there are Access Control shortcodes available that will restrict access to specific post contents based on a User role. For example:

[toolset_access role="arbeidstaker" operator="deny"]
Users who are 'arbeidstaker' role CANNOT see this content. Other roles CAN see this content.
[/toolset_access]

You can use the Access button above any class text editor area to insert this type of shortcode control. See the screenshots here.

If your site does not use Toolset Access, you may be able to use conditional shortcodes or a conditional block in the block editor. The format for a conditional shortcode that tests the current User role is like this:

[wpv-conditional if="( '[wpv-current-user info="role"]' ne 'arbeidstaker' )"]
Users who are 'arbeidstaker' role CANNOT see this content. Other roles CAN see this content.
[/wpv-conditional]

Let me know if these approaches will not work for some reason, and we can discuss other options.

#1932467

Hi Christian,
I only want to hide the button, not restrict access.

When a admin user edits the post, the values will be visible to a role with less rights. Only a user with certain roles should be able to see / view the button itself.

The other users should not see the button, but they should se the content.

#1933193

Hi,
This now partly works! Great!

This is the code:
[/wpv-conditional]
<div>[wpv-conditional if="( '[wpv-current-user info="role"]' ne 'barnehage' )"]<button class="btn btn-rediger btn-block" type="button" data-toggle="collapse" data-target="#collapseExample[wpv-post-id]" aria-expanded="false" aria-controls="collapseExample[wpv-post-id]">
Rediger kontrollpunkt
</button>[/wpv-conditional]

Problem is that I use mutiple roles. Almost all users have mutiple roles, and I need to show this button if the user have additional roles.

If user is A role, the button should be hidden
If user is A role +B role the button should be visible.

Is this possible to achive?

#1933735

To test if a user has one or more roles assigned, you'll need some custom code. I don't have a solution available that easily tests the number of roles, but I do have a solution available that tests any role by slug. Another ticket in the forum discusses this custom code solution: https://toolset.com/forums/topic/conditional-display-based-on-user-role-not-working-if-user-has-multiple-roles/

You'll need to add this custom shortcode to your child theme's functions.php file, or to a new custom code snippet set to run everywhere and activated in Toolset > Settings > Custom Code tab:

// shortcode for testing one or more of possible multiple User roles in a conditional
// https://toolset.com/forums/topic/hide-a-button-in-view-for-certain-roles/
add_shortcode('roles', function ($atts = []) {
 
    // provide defaults
    $atts = shortcode_atts(
        array(
            'role' => '',
        ),
        $atts
    );
 
    $return = '0';
    $current_user = wp_get_current_user();
 
    $roles = explode( ',', $atts['role'] );
    foreach ($roles as $role) {
        if ( in_array( $role, $current_user->roles ) ){
            $return = '1';
        }
    }
 
    return $return;
});

Then add shortcode name roles in Toolset > Settings > Front-end Content: Third party shortcode arguments.

After that, you'll be able to use the roles shortcode in a conditional that tests multiple roles like so:

[wpv-conditional if="( '[roles role="barnehage"][/roles]' eq '1' ) AND ( '[roles role="administrator,author,editor"][/roles]' eq '1' )"]
show this content to users who have the 'barnehage' role AND one of these other roles: administrator, author, or editor.
[/wpv-conditional]

You must include the slugs of all the other acceptable roles in a comma-separated list where I have administrator,author,editor in my example.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.