Skip Navigation

[Gelöst] Displaying content based on Access group

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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.

Relevant Documentation:
https://toolset.com/documentation/user-guides/access-control/access-control-texts-inside-page-content/

This support ticket is created vor 4 Jahre, 1 Monat. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Artikel
#1541063

Hi,

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...

I guess this solution is similar: https://toolset.com/forums/topic/conditional-display-based-on-user-role-of-post-author/#post-543582, but in my case it's not based on author, but logged in user... So, don't know how much it's different...

Thank you.

P.S. Just for your info, here's the code so far (with the other conditions... nothing for user roles yet)...
[wpv-conditional if="( $(wpcf-copyright-type) ne '2' ) AND ( '[types field="droits-publication" option="1"][/types]' ne '1' ) AND (( '[types field="droits-publication" option="0"][/types]' eq '1' ) OR ( $(wpcf-date-edition).item(@numeros-pi-articlepi.parent) + 63072000 lt 'TODAY()' ))"]<a href="#[wpv-post-slug]">[/wpv-conditional][wpv-post-title][wpv-conditional if="( $(wpcf-copyright-type) ne '2' ) AND ( '[types field="droits-publication" option="1"][/types]' ne '1' ) AND (( '[types field="droits-publication" option="0"][/types]' eq '1' ) OR ( $(wpcf-date-edition).item(@numeros-pi-articlepi.parent) + 63072000 lt 'TODAY()' ))"]</a>[/wpv-conditional][wpv-conditional if="( $(wpcf-personne-interviewee) ne '' )"] - Interview de [types field='personne-interviewee'][/types][/wpv-conditional][wpv-conditional if="( '[wpv-post-title item="@auteur-pi-articlepi.parent"]' ne 'NULL' )"] Par [wpv-post-title item="@auteur-pi-articlepi.parent"][/wpv-conditional]

#1541279

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?

#1545187

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...

Thank you.

#1545993

Shane
Supporter

Languages: Englisch (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nelson,

Christian is currently unavailable today but will be back tomorrow to continue assisting with this ticket.

Thanks,
Shane

#1547185

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:

function get_user_roles_func( $atts )
{
  $a = shortcode_atts( array(
      'userid' => '',
      'separator' => ','
  ), $atts );
  $userdata = get_user_by($a['separator'], $a['userid']);
  $user_roles = implode(',',$userdata->roles);
  return $user_roles;
}

You can use it like this:

[get-user-roles userid="1234"][/get-user-roles]

Results:

author,co-worker

You would change 1234 to some User ID.

You can add the separator attribute if you want to customize the separation of items in the list:

[get-user-roles userid="1234" separator=" | "][/get-user-roles]

Results:

author | co-worker
#1547557

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!!

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