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
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
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]
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/
Original question / issue is resolved now. Thank you!