Skip Navigation

[Closed] Roles and [toolset_access

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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 3 replies, has 2 voices.

Last updated by Mateus Getulio 3 months, 3 weeks ago.

Assisted by: Mateus Getulio.

Author
Posts
#2725527

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

[toolset_access role="Site Registered User"][/toolset_access]

When they pay for a subscription they get the additional role of Site Paying Member, but also the first access check display.

[toolset_access role="Site Paying Member"][/toolset_access]

I can't work with this either, as it only checks the main role
[wpv-conditional if="('[wpv-current-user info='role']' eq 'site_paying_member')"]

#2725998

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hi there,

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

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

    // provide defaults
    $atts = shortcode_atts(
        array(
            'role' => '',
        ),
        $atts
    );

    $return = '0';
    $current_user = wp_get_current_user();

    $roles = explode( ',', $atts['role'] );
    foreach ($roles as $role) {
        if ( in_array( $role, $current_user->roles ) ){
            $return = '1';
        }
    }

    return $return;
});

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.

Can you please check if it helps?

#2732040

From looking at the code, it is the same as using toolset_access

[toolset_access role="Administrator,Guest"]
[fvplayer id="[wpv-post-field name='wpcf-video-member' item='@workout-session-video.child']" width="100%" height="100%" popup="1" startend="00:00:00-00:00:45" lightbox="true;text" caption="[wpv-post-title item='@workout-session-video.child']"]
[/toolset_access]

[toolset_access role="Site Registered User"]
[fvplayer id="[wpv-post-field name='wpcf-video-member' item='@workout-session-video.child']" width="100%" height="100%" popup="1" startend="00:00:00-00:01:30" lightbox="true;text" caption="[wpv-post-title item='@workout-session-video.child']"]
[/toolset_access]

[toolset_access role="Site Paying Member"]
[fvplayer id="[wpv-post-field name='wpcf-video-member' item='@workout-session-video.child']" width="100%" height="100%" lightbox="true;text" caption="[wpv-post-title item="@workout-session-video.child"]"]
[/toolset_access]

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.

#2732475

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

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:

[toolset_access role="Administrator,Guest"]
[fvplayer id="[wpv-post-field name='wpcf-video-member' item='@workout-session-video.child']" width="100%" height="100%" popup="1" startend="00:00:00-00:00:45" lightbox="true;text" caption="[wpv-post-title item='@workout-session-video.child']"]
[/toolset_access]

[toolset_access role="Site Registered User"]
[fvplayer id="[wpv-post-field name='wpcf-video-member' item='@workout-session-video.child']" width="100%" height="100%" popup="1" startend="00:00:00-00:01:30" lightbox="true;text" caption="[wpv-post-title item='@workout-session-video.child']"]
[/toolset_access]

[wpv-conditional if="('[custom_role_check role="Site Registered User"]' eq '1')"]
    [toolset_access role="Site Registered User"]
    [fvplayer id="[wpv-post-field name='wpcf-video-member' item='@workout-session-video.child']" width="100%" height="100%" popup="1" startend="00:00:00-00:01:30" lightbox="true;text" caption="[wpv-post-title item='@workout-session-video.child']"]
    [/toolset_access]
[/wpv-conditional]

Please give it a try, if it doesn't work, to suggest the best way to achieve this, I'll need to see how this is set up in the admin area.

Can you please share temporary admin login details along with a link to a page where this conditional content can be seen?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Mateus

The topic ‘[Closed] Roles and [toolset_access’ is closed to new replies.