Skip Navigation

[Resolved] Conditional output on a View displaying Users based on User Role?

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)

This topic contains 11 replies, has 2 voices.

Last updated by lesleeM 1 year, 9 months ago.

Assisted by: Nigel.

Author
Posts
#2595819

We have a page where we're creating Member Profiles to display based on custom User fields. We have this mostly done. But we would like to add a designation for Ride Leader for all users that have been assigned the Ride Leader role on the site. In the conditional output dialog box, I'm not seeing any fields based on user roles. I'm guessing this can be done by manually creating the conditions, but I'm not sure what code to use for this. Any assistance you can provide would be appreciated.

#2596207

Nigel
Supporter

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

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

Screenshot 2023-04-18 at 15.14.25.png

Hi there

Can you please clarify the context here?

Because it sounds like you have a View to display users (which would use the legacy editor, user Views are currently not possible with the block editor), and when you create a user View the first thing you do is specify the role the users queried by the View should have (screenshot).

You must be talking about something different. Could you please explain?

#2597393

OK, yeah, we are displaying all users that have a Membership role first. But then as a subset of those Member users, we also want to set up a special designation for any Members that also have a Ride Leader role assignment. All Ride Leaders are Members, but not all Members are Ride Leaders. In the view, we want to have conditional output to give special recognition to Members that are Ride Leaders. So there needs to be that secondary conditional output based on a different user role.

#2597431

Nigel
Supporter

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

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

OK, got it.

For this you'll need to register a custom shortcode that checks whether a specified user has a specified role, returning true or false (actually, 1 or 0).

You can add a code snippet at Toolset > Settings > Custom Code with the following:

/**
 * Register shortcode to test if a particular user has a particular role
 * 
 * @param $atts['userid']
 * @param $atts['role']
 * 
 */
add_shortcode('user_has_role', function ($atts = []) {

    if ( ( !isset($atts['userid']) && !is_numeric($atts['userid']) ) || ( !isset($atts['role']) ) )
        return 0;

    $user = get_userdata( $atts['userid'] );

    if ( in_array( $atts['role'], (array) $user->roles ) )
    {
        return 1;
    } else {
        return 0;
    }

});

You will then need to register the custom shortcode at Toolset > Settings > Front end content so that it can be used in conditional tests.

Then in the output of your View you would use the custom shortcode inside a conditional shortcode something like this:

          [wpv-conditional if="( '[user_has_role role='ride-leader' userid='[wpv-user field='ID']']' eq '1' )"]
          <p>This user is a Ride Leader</p>
          [/wpv-conditional]
#2598049

Is there anything I need to customize in the top code? In the bottom bit, I changed role='ride-leader' to role='ride_leader' to match how it shows it in the Role Editor.

I set up the top code in Custom Code in Settings and activated it. I added the bottom code to our view. It isn't displaying the desired output. So I've likely done something wrong, or something may need to be customized in the top code.

#2598409

Nigel
Supporter

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

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

OK, first check the custom shortcode is working as expected by just adding the shortcode directly to the output (instead of as the if argument of the wpv-conditional shortcode).

It should display 1 or 0 depending on whether the current user in the loop has the ride_leader role or not...

#2599295

I eliminated the conditional and used just:

[user_has_role role='ride-leader' userid='[wpv-user field='ID']']

This is displaying a 1 and a 0 on all members in the view as it should.

#2599427

Nigel
Supporter

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

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

So the custom shortcode is working.

Did you remember to register this custom shortcode at Toolset > Settings > Front end content?

If so then try turning debug mode on for the conditional shortcode with the debug="true" attribute, like so:

[wpv-conditional if="( '[user_has_role role='ride-leader' userid='[wpv-user field='ID']']' eq '1' )" debug="true"]
<p>This user is a Ride Leader</p>
[/wpv-conditional]

That will show what is being evaluated when you visit it on the front end.

Can you screenshot that and show me?

#2599781

Re: Register the shortcode at Toolset > Settings >Front end content.

I misread that and mistook what you were saying there as being about activating the custom code snippet. I'm not sure how to do this in the Front end content area.

Am I putting all of this in the section that says "Third-party shortcode arguments"

[user_has_role role='ride-leader' userid='[wpv-user field='ID']']

?

Or do I just put [user_has_role] in there?

#2599989

Nigel
Supporter

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

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

Sorry for the confusion.

Just "user_has_role", no square brackets. It is just the name of the shortcode required.

#2600095

That was it! It's working now. Thanks so much for the assistance. Sorry for my misread causing the extra back and forth there.

#2600097

My issue is resolved now. Thank you!