Skip Navigation

[Resolved] Configuring access using Combined Roles

This support ticket is created 2 years, 8 months ago. 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.

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 1 reply, has 2 voices.

Last updated by Waqar 2 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#2317741

Is there a way to configure access to certain pages basically using an "AND" rule?

Our website allows users to create a free account, but we want to create paid content for certain roles.

For example, let's suppose that Jane is simply a "User"; Mary is a "User" and a "Member"; and Beth is a "User", "Member", and "TicketHolder".

I want to configure access to a page with content for people like Beth who are logged in, a paid member, and paid for a ticket to access the content.

In the documentation, Toolset allows you to pick and choose which roles see what, but it seems to operate more like "OR". Any help would be appreciated, thanks! If responses are emailed, please CC me using alaythea@cappa.net.

#2318295

Hi,

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

If your goal is to check for the attachment of multiple user roles with the currently logged-in user, you'll need a custom shortcode:


add_shortcode('check_user_roles', 'check_user_roles_func');
function check_user_roles_func($atts) {
	$roles = $atts['roles'];

	if(!empty($roles)) {
		$roles_to_check = explode(',', $roles);
	}
	
	if( is_user_logged_in() ) {
		$user = wp_get_current_user();
		$current_user_roles = ( array ) $user->roles;

		$containsAllRoles = !array_diff($roles_to_check, $current_user_roles);
		if($containsAllRoles) {
			return '1';
		}
		else
		{
			return '0';
		}
	}
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Note: Please also add "check_user_roles" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

This shortcode can accept the comma-separated list of target user role slugs to check in the 'roles' attribute and then return '1' if all target roles are attached with the current user or '0' if not.

Example:


[check_user_roles roles="subscriber,author,editor"]

To conditionally show some content to a user who has Subscriber, Author, and Editor roles, the condition will look like this:


[wpv-conditional if="( '[check_user_roles roles="subscriber,author,editor"]' eq '1' )"]
The current user has the Roles: Subscriber, Author, and Editor
[/wpv-conditional]

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar