Skip Navigation

[Resolved] Role based conditional display not working for one of the two roles.

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 9 replies, has 2 voices.

Last updated by lesleeM 11 months, 4 weeks ago.

Assisted by: Waqar.

Author
Posts
#2608987

I have set up a section of our Ride template page to conditionally display a Post form for Ride Notifications. The conditional is set as follows:

Current user role is equal to role Administrator.
OR
Current user role is equal to role Ride Leader.

When I click Edit Conditions, it shows the following under Advanced Editor:

( ( ( '[wpv-current-user info="role"]' eq 'administrator' ) OR ( '[wpv-current-user info="role"]' eq 'ride_leader' ) ) )

This is working fine for the Administrator role, but it is not working for the Ride Leader role. The content is not displaying for someone whose user account has the Ride Leader role. It is possible that ride_leader is not the correct way to define this. I tried changing it to ride-leader and that didn't work either. Where do I find the role slug?

#2609105

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

primary-user-role.png

Hi,

Thank you for contacting us and I'd be happy to assist.

The conditional statement that you've shared works as expected on my test website.

Based on this, I'll recommend the following troubleshooting steps:

1. While logged in as the Ride Leader role user that you're testing with, visit a page where the [wpv-current-user info="role"] shortcode is in use and make sure to copy the exact text that is returned by the shortcode.

2. While logged in as an Administrator user, visit WP Admin -> Users and open the profile of the Ride Leader role user that you're testing with. If multiple user roles are assigned to this user, make sure that the 'Ride Leader' role is selected as the primary one.
( example screenshot attached )

I hope this helps and let me know how it goes.

regards,
Waqar

#2609233

1) OK, when logged in as a Ride Leader user and visiting a Ride page where the conditional display should show a post form, there is nothing displaying in that spot. There is no text returned by the shortcode that I see. There is nothing in the source code either. It is behaving the same way it correctly does for a Guest user in not displaying anything in that spot.

2) I think you're onto the issue here. This user account has two roles. One is "Ride Leader" the other is "Florida Freewheelers Membership". The "Florida Freewheelers Membership" one is the one that is in the dropdown which I assume means that is the primary one. I'm worried about changing the primary role to "Ride Leader" here because I think that might break other functionality that is currently working. Plus I'm worried that I may have to do this change manually for every Ride Leader user. If this role not being the primary role is the reason the conditional isn't working, is there anything we can do to address that problem rather than changing the primary role for every Ride Leader user?

#2609245

I went ahead and changed it on one user and that worked for me.

We have one other Ride Leader user that had her Ride Leader role set as primary, so that made me more comfortable that changing it wouldn't impact the membership functionality. That Ride Leader previously could not see the form, but they did see all the other content in that conditional display. So the conditional was working as it should for her. I believe the reason she couldn't see the form was because I hadn't set the permission setting for the Ride Leader user to have permission to create content using that form. I changed that and now have asked if she is able to see the form. I'm guessing that will solve it for her because I am able to see the form with my user account where I changed Ride Leader to primary. I'll let you know what she says when I get a reply.

But my concern still remains about having to manually change every Ride Leader user to having that set as the primary role to get this to work. I'm not so worried about having to do that work now for all our current Ride Leader users. I'm just concerned about having to manually do it for every future Ride Leader. I'll remember to do it, but if I'm ever no longer admin on this site, I'd be concerned about a new admin having to remember that every time a new Ride Leader is added. So I'd still like to see if there is any way to code the conditional to where it will pick up on the Ride Leader role even if it is not the primary role for that user.

#2610121

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing this update.

If you don't want to change the primary roles of the users, you'll need a custom shortcode that can check if a particular user role is assigned to the current user or not.
( regardless of the primary or the secondary position )

You'll find an example code snippet for a shortcode like this, in this forum reply:
https://toolset.com/forums/topic/conditional-logged_in-and-user-role/#post-2491295

#2610191

OK, I went ahead and resaved all the Ride Leaders currently in our system to make sure Ride Leader is their primary role. So we're good for all current users with that. I'll also add this shortcode so that step won't need to be done again or remembered by any future admins. Thanks a bunch.

Let me keep this ticket open until I apply and activate the shortcode and add the conditional to the display of the form on the template though. I'll get this done shortly. I just want to make sure I don't have any problems with this. It looks straight-forward enough for me to be OK setting it up myself.

#2610389

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Sure, please take your time and let me know if you have any follow-up questions.

For new questions or concerns, you're welcome to start a new ticket.

#2610641

With the shortcode, I'm seeing that we already have a shortcode that you guys helped us with previously. That code is below:

-------------------------------------------------------------------------------------

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;
}

});

-------------------------------------------------------------------------------------

Is this OK to use or should I set up another one using what you provided?

#2611041

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Yes, both these shortcodes are technically the same.

If you already have this one registered on the website, you can keep using it. You don't need to add the one from the other support thread that I referenced.

#2613171

My issue is resolved now. Thank you!

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