On a custom post type i have created a front-end template.
Within that template i want to display 2 pieces of conditional content based on the Current users role as follows:
Content piece 1 - IF the current user role is "Premium Subscriber" then display a "new message" toolset form
- I would expect it to display the form if the user is a Premium Subscriber
- If the user is not a Premium Subscriber then the block is not shown
Content piece 2 - IF the current user role is "Agency Member" then display a Contact 7 form
- I would expect it to display the form if the user is an Agency Member
- If the user is not a Agency Member then the block is not shown
I cannot get Content Piece 1 or 2 to display correctly based on testing with 2 users, 1 is Premium Subscriber and the other is Agency Subscriber
Hello and thank you for contacting the Toolset support.
You need to compare against the role slug instead of the role name. What if you try with role "agency_member" instead of "Agency Member"?
You can get the role's slug by hovering over it in Toolset->Access Control->Custom Roles. Check this screenshot hidden link
Remember that you can activate debugging on the conditional block to get more details on the frontend. Check this screenshot hidden link
I have turned on debug mode (see attached)
I have also tried comparing a "current user role to static data of agency_member or premium_subscriber
Both of the above roles are secondary roles. The premium role is Subscriber
The user i am testing with has a role of Subscriber and additional roles of Agency member I have also tried the premium Subscriber role
In all circumstances it does not work as expected
If i change the primary role to either agency_member or premier_subscriber i can get the function to work.
My understanding of the conditional function is https://toolset.com/course-lesson/setting-up-custom-roles-for-members is that it looks at the primary role and any additional but this does not appear to be working
The conditional block will check against the primary role, it won't check any additional roles.
In the article that you shared, I don't see anything that says additional roles will be evaluated inside the conditional block. Can you show me what part says that? I'll escalate to our documentation team to correct it.
As you have users with multiple roles, you need to use a custom shortcode or custom function. I run a small test locally and I come up with the following function:
function user_has_role( $role = '', $user_id = null){
if ( is_numeric( $user_id ) ) {
$user = get_user_by( 'id', $user_id );
} else {
$user = wp_get_current_user();
}
if ( empty( $user ) )
return false;
return in_array( $role, (array) $user->roles );
}
You can add it to your child theme functions.php file or in Toolset->Settings->Custom Code. But make sure the snippet is active.
https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
Then you will have to register the function in Toolset->Settings in order to use it inside conditional blocks. Check this article https://toolset.com/documentation/programmer-reference/views/using-custom-functions-in-conditions/
Then you can build the condition just like this screenshot hidden link
Hi Thanks for this.
Screenshot 9 attached is where i read in the documentation that it will look for secondary roles. I may have missread it.
I have implemented it as suggested but it doesn't appear to work.
I believe i have added it correctly see screenshots
Although when i added the Front end content part all i added was "user_has_role" do i need to add something else here? i am not a developer and therefore i do not know what to add here
Yes, you actually misread the documentation, it is about permissions instead of roles. A role has one or multiple permissions. Permissions are actually called capabilities in WordPress documentation. Check this article https://wordpress.org/support/article/roles-and-capabilities/
I believe that the snippet is still not activated. Check the status "Inactive" in this screenshot hidden link
Make sure that the snippet is activated. If it still does not work, allow me temporary access and let me the URL in the backend of this page/content template and I'll check it further. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
My issue is resolved now. Thank you!