Skip Navigation

[Resolved] Conditional display based on user role not working if user has multiple roles

This support ticket is created 4 years, 9 months 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 4 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#1574855

I am trying to: If a user has multiple assigned to them e.g. Subscriber & Author, the following code does not work:

[wpv-conditional if="( '[wpv-current-user info="role"]' eq 'subscriber' ) OR ( '[wpv-current-user info="role"]' eq 'author' )"]
  
[cred_field field='privacy' force_type='taxonomy' output='bootstrap' display='select' single_select='true']
  
[/wpv-conditional]

Nothing is displayed to either user.

Link to a page where the issue can be seen:

I expected to see:
I expected to see the fields inside the conditional display group for a user in with one of the listed roles.

Instead, I got:
No display at all

#1575623

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tim

Support for multiple roles was added to Access but the wpv-current-user shortcode only recognises the first role, and even if it returned all roles it would be difficult to test that with the normal comparisons available to the wpv-conditional shortcode.

You'll need a custom shortcode for this, I think.

Take the following code which registers a shortcode "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;
});

It will return '1' if any of the specified roles apply to the current user.

You would use it like this:

[roles role='author']  // just test if the current user has the 'author' role (amongst others)
[roles role='editor']  // test if they have the editor role
[roles role='author,editor'] // test if they have either the author or the editor role

To then use that in a wpv-conditional shortcode, don't forget to to register the custom shortcode at Toolset > Settings > Front-end Content.