[Resolved] Displaying content based on Access group
This thread is resolved. Here is a description of the problem and solution.
Problem: I would like to incorporate conditionals based on User roles, along with other factors like custom field values.
Solution: For simplicity, it's best to use Access Control shortcodes first to separate Users by role. Then within those Access Control shortcodes, implement your various other conditional to determine what content should be shown. This will lead to some duplication of code, but in exchange the code is more manageable.
I'd like to use a condition to show a link (or maybe eventually content) based on the the user group the current user is... Let me try to explain.
I already have conditions in my View to show links depending on other criteria. Now, what we need to add are paying subscribers. So, I created two groups in Toolset's Access: co-workers, which will have access to everything (in read-only), paying subscribers that have access to everything that doesn't have any group specified + certain other elements marked for that group, and public (default, not a group) that have access to everything that doesn't have a group associated.
I have associated those two groups to some testing posts...
Now, what I need is to be able to either show a link (or not) based on the role the logged in user has... There's no way to access Access' roles in View, unfortunately...
It's easier and more straightforward to use Access Control shortcodes to restrict content than to try to implement user roles in conditional HTML: https://toolset.com/documentation/user-guides/access-control/access-control-texts-inside-page-content/
Choose which roles should see some content or not see some content, and the system will generate the proper shortcodes for you. Is there a specific reason Access Control shortcodes cannot be integrated in your case?
Well, I've tried it... There might be something that I don't understand, but it's not that easy...
Here's what I would need to check (divided by sections with indentation to make it easier to understand):
[wpv-conditional if="( $(wpcf-copyright-type) ne '2' ) // copyright-type custom field different than @
AND
( '[types field="droits-publication" option="1"][/types]' ne '1' ) // No Web publication is unchecked
AND
(
( '[types field="droits-publication" option="0"][/types]' ne '1' ) // Wait 2 years before publishing is unchecked
OR
( $(wpcf-date-edition).item(@numeros-pi-articlepi.parent) + 63072000 lt 'TODAY()' ) // Wait 2 years is checked, but it's been more than two years
OR
( '[toolset_access role="Subscriber" operator="allow" raw="true"]') // if Wait 2 years is checked, but the user is a subscriber... but where would I close it...?
)
OR
( '[toolset_access role="Administrator,Co-worker" operator="allow" raw="true"]') // no matter all the above, if it's an administrator or a co-worker, we always show it
"]
Content
[/toolset_access][/wpv-conditional]
So, as you can see, there are A LOT of variables to consider... So, I need to integrate the Access roles as any other conditions... I probably don't have the right syntax, and even if I would, I don't really know where I would close the [/toolset_access] shortcode... That's why I was expecting a code that I could add to a condition (like 'wpv-user' or 'wpv-user-data'...).
Don't know if I could create a function that I could use that would return the user's role and then use it for comparison...? Of course, that would mean that the custom Access roles are taken into consideration in the function...
Okay the Access Control shortcode method looks different because it is not integrated with the conditionals. You will use Access Control shortcodes to break things up by role first, then include conditional code within those Access Control shortcode blocks. This could lead to a bit of code duplication for CONTENT, but it is easier to read and manage in my opinion. For example, I have simplified the conditionals a bit for clarity:
<!-- ALWAYS SHOW THE CONTENT TO ADMINS AND CO-WORKERS -->
[toolset_access role="Administrator,Co-worker" operator="allow" raw="true"]
CONTENT
[/toolset_access]
<!-- SHOW THE CONTENT TO SUBSCRIBERS WHEN THESE CONDITIONS ARE MET -->
[toolset_access role="Subscriber" operator="allow" raw="true"]
[wpv-conditional if="( $(wpcf-copyright-type) ne '2' )
AND
( '[types field="droits-publication" option="1"][/types]' ne '1' )
... your conditions will continue here ...
]
CONTENT
[/wpv-conditional]
<!-- SHOW THE CONTENT TO ALL ROLES OTHER THAN ADMINS, CO-WORKERS, AND SUBSCRIBERS WHEN THESE CONDITIONS ARE MET -->
[toolset_access role="Administrator,Co-worker,Subscriber" operator="deny" raw="true"]
[wpv-conditional if="( $(wpcf-copyright-type) ne '2' )
AND
( '[types field="droits-publication" option="1"][/types]' ne '1' )
... your conditions will continue here ...
]
CONTENT
[/wpv-conditional]
[/toolset_access]
Don't know if I could create a function that I could use that would return the user's role and then use it for comparison...?
I have a custom shortcode that will return a comma-separated list of user role slugs for any User by ID:
I used the first option you provided... A little sad that we can't use it in a condition, as it would make less code, but it stills works great (and will probably be easier to understand for my client)...
Thanks a lot! I'm always amazed at the level of support for Toolset and WPML!!