Skip Navigation

[Resolved] Conditional with a Custom 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 11 replies, has 1 voice.

Last updated by Timothy 6 days, 9 hours ago.

Assisted by: Minesh.

Author
Posts
#2817817

Hi,

I've created 2 new custom roles with Access "For Review" and "Lead Role", and I have a legacy view where I display a table of user details for these 2 roles.

I'm using this shortcode to get the user role :
[get_user_role user_id='[wpv-user field="ID"]']

I've successfully created one table that can display the user details for both these roles, but I'd like to display 2 separate tables, one that has the details of users with role For Review and one with the role Lead Role. I've tried various conditionals like these but none work:

[wpv-conditional if="( '[get_user_role]' eq 'for_review' )"]

[wpv-conditional if="( '[get_user_role user_id='[wpv-user field="ID"]']' eq 'for_review' )"]

Tim

#2817887

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As you are using the custom shortcode, you will have to register the custom shortcode at:
=> Toolset => Settings => Front-end Content => Third-party shortcode arguments section

There, simply add your shortcode name to the Third-party shortcode arguments section. You can register the shortcode name get_user_role

More info:
- https://toolset.com/documentation/legacy-features/views-plugin/using-shortcodes-in-conditions/#checking-custom-shortcodes

Alternatively, you can also use the [toolset_access] shortcode. For example:

[toolset_access role="for_review" operator="allow"]
// add whatever you want to display for "for_review" role 
[/toolset_access]

[toolset_access role="for_review" operator="deny"]
// add whatever you want to display for "for_review" role when there is no rights 
[/toolset_access]

Please check the following related ticket.:
- https://toolset.com/forums/topic/user-field-access-controls/

#2817892

Thanks, but I had already registered the shortcode in Toolset Settings. And I just tried the Access shortcode you provided but those also do not work.

Tim

#2817893

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

can you please share problem URL and tell me where exactly you wnat to display what and send me admin access details and let me review what's going wrong with your setup.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2817910

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've added the following code to "Custom code" section offered by Toolset with the code snippet namely "toolset-custom-code":

function tssupp_filter_user_query($query_args, $view_settings, $view_id) {
    
  	global $WP_Views;

  
  	$role_to_display = $WP_Views->view_shortcode_attributes[0]['role'];
  
 	if($view_id==82179){
      	$query_args['role__in'] = array($role_to_display);
    }
    return $query_args;
}
add_filter('wpv_filter_user_query', 'tssupp_filter_user_query', 101, 3);

And you need to add the view to Beaver Builder as given under:
=> hidden link

[wpv-view name="client-profile-view" role='for_review']

[wpv-view name="client-profile-view" role='lead_role']

Where as you can see we are passing the role value using the shortcode attribute name "role".

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query
- https://toolset.com/documentation/legacy-features/views-plugin/passing-arguments-to-views/#controlling-the-filter-with-shortcode-attributes

#2817911

Awesome! This works.

So that code allows me to use role as an attribute in the view shortcode? Just curious though why the conditionals won't work within the View? In case I need to show/hide details in one table based on their role is this not possible?

Tim

#2817916

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

So that code allows me to use role as an attribute in the view shortcode?
==>
Yes.

Just curious though why the conditionals won't work within the View? In case I need to show/hide details in one table based on their role is this not possible?
===>
That is not the right approach, either you use the solution I shared, if you do not want to use that then you can create two views, with one view you should set to use th role "for_review" and setup the view's loop editor and create another view for the role "lead_role" and setup its view's loop editor.

#2817923

I prefer the solution you shared, just curious why views has conditional options like this but they don't work:
[wpv-conditional if="( '[get_user_role]' eq 'for_review' )"]

Tim

#2817924

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - the conditional you are talking about will apply per post and you want to categoriese bulk posts belongs to role1 and role2. In your case "for_review" and "lead_role".

#2818045

Ok, it's just strange that the conditionals seemed to work for the standard roles, like:

[wpv-conditional if="( '[get_user_role]' eq 'administrator' )"]
I am an admin
[/wpv-conditional]

#2818047

Even more strange, today the conditional for my custom roles are working:

[wpv-conditional if="( '[get_user_role user_id='[wpv-user field="ID"]']' eq 'for_review' )"]
Accept
Decline
[/wpv-conditional]

#2818062

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm not sure but you go for that approach I shared as conditional statements were not suitable.

It could be possibally due to cache or anything.

#2818192

Thank you