Skip Navigation

[Resolved] Error when "check_if_allowed_by_user_role" shortcode is displayed without log in

This support ticket is created 3 years, 3 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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by davidH-33 3 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#2190099
toolset-shortcode-error.jpg

Tell us what you are trying to do?

Waqar provided me 2 days ago this custom function / shortcode to be able displaying content based on the user role:
https://toolset.com/forums/topic/displaying-view-that-shows-only-items-related-to-user-role-of-logged-user/

function check_if_allowed_by_user_role_func( $atts ) {
    // Attributes
    $atts = shortcode_atts(
        array(
            'user_id' => '',
            'field_slug' => '',
        ),
        $atts
    );
 
    // get user's roles
    $user_info = get_userdata((int)$atts['user_id']);
    $user_roles = $user_info->roles;
 
    // get role from the current post's custom field
    $role_from_field = types_render_field( $atts['field_slug'], array( ) );
 
    // check if the role from the custom field is attached with the user
    if (in_array($role_from_field, $user_roles)) {
        // return 1 if found
        return 1;
    }
    else
    {
        // return 0 if not found
        return 0;
    }
}
add_shortcode( 'check_if_allowed_by_user_role', 'check_if_allowed_by_user_role_func' ); 

Shortcode:

[wpv-conditional if="( '[check_if_allowed_by_user_role user_id='[wpv-user field='ID']' field_slug='userrolefilmonline']' eq '1' )"]
Allowed!
[/wpv-conditional]

The conditional shortcode works fine in view as long as user is logged in. Is there any chance to modify the function / shortcode to get rid of this error?
"Warning: in_array() expects parameter 2 to be array, null given in /www/doc/www.acfk.cz/www/wp-content/toolset-customizations/check_if_allowed_by_user_role.php on line 26"
hidden link

Is there any documentation that you are following?
https://toolset.com/forums/topic/displaying-view-that-shows-only-items-related-to-user-role-of-logged-user/

Is there a similar example that we can see?
hidden link

What is the link to your site?
hidden link

#2190485

Hello,

The [wpv-user] shortcode will display information for the current logged-in user, in your case, you can try these:
modify this line from:

$user_info = get_userdata((int)$atts['user_id'])

To:

if($atts['user_id']){
	$user_info = get_userdata((int)$atts['user_id'])
}else{
	return 0;
}

And test again

#2192297

Thank you, the issue with error message for non-logged users has been fixed with your advice. However I noticed that the shortcode for conditional displaying based on user roles works fine only in the View. When I put this shortcode directly into Elementor shortcode or text widget it outputs 0 even on pages of items with the right custom field text value. So I'm not sure If I have to change the conditional shortcode for using it in Elementor in some way.

[wpv-conditional if="( '[check_if_allowed_by_user_role user_id='[wpv-user field='ID']' field_slug='userrolefilmonline']' eq '1' )"]
Allowed!
[/wpv-conditional]

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/custom-shortcode-in-elementor-shortcode-or-text-widget/

#2192435

I assume the original question of this thread is resolved, according to our support policy, we prefer one question on ticket, for other Elementor questions, please check the new thread here:
https://toolset.com/forums/topic/custom-shortcode-in-elementor-shortcode-or-text-widget/

#2192613

Original question / issue is resolved now. Thank you!