The WordPress plugin Paid Member Subscriptions ADDS a role after successfully subscribing to our website. Instead of changing the role it adds, which means that our coding logic no longer works, or rather will display two pieces of content. The question is, how do I overcome this?
When the user registers, they are the role Site Registered User
Support for multiple roles was added to Access but the shortcode only recognizes the first role, and even if it returned all roles it would be difficult to test that with the normal comparisons available to the wpv-conditional shortcode.
You'll need a custom shortcode for this, I think.
Take the following code which registers a shortcode "roles".
It will return '1' if any of the specified roles apply to the current user.
You would use it like this:
[roles role='author'] // just test if the current user has the 'author' role (amongst others)
[roles role='editor'] // test if they have the editor role
[roles role='author,editor'] // test if they have either the author or the editor role
To then use that in a wpv-conditional shortcode, don't forget to to register the custom shortcode at Toolset > Settings > Front-end Content.
When the user has the role Site Paying Member, it also has the role Site Registered User, so it would display the output twice, once for Site Registered User, and once for Site Paying Member.
I used to use a conditional statement and check the main role, but the new plugin I am using is not changing the main role, it is adding an additional role, so the user remains a Site Registered User and additionally Site Paying Member.
I see, since the issue is only happening for 'Site Paying Member', one option we have would be to use the regular Toolset check for 'Site Registered User', which is working, and for the conditional that is bringing results wrongly for Site Registered User, we can change it testing if it is the only role assigned:
Please replace the custom code above with this:
add_shortcode('custom_role_check', function ($atts = []) {
// Provide defaults
$atts = shortcode_atts(
array(
'role' => '', // The role to check
),
$atts
);
$current_user = wp_get_current_user();
$role_to_check = $atts['role'];
// Check if the user has exactly one role and it matches the specified role
if (count($current_user->roles) === 1 && in_array($role_to_check, $current_user->roles)) {
return '1';
}
return '0';
});
Add it as a shortcode and then your conditional could look something like this: