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