php - Show content based on multiple user roles in WordPress -
i looking display content based on multiple different user roles.
the aim display content 2 or 3 different user roles, , block other user roles, displaying message logged in users.
so far, have following:
<?php global $user_login, $current_user; get_currentuserinfo(); $user_info = get_userdata($current_user->id); $roles = array ( 'administrator', 'subscriber', ); if (is_user_logged_in() && in_array( $roles, $user_info->roles)) { //content here } else { // aren't logged in, show them login form } ?>
at moment, issue having seems code looking both administrator , subscriber roles @ same time , result, if statement not satisfied , login form shows.
if change $roles 'administrator' or 'subscriber', works fine.
so, how search through array display either role, not of them.
thanks
you use: array_intersect (link)
array_intersect check between 2 array see if needle ($roles
) exists in haystack ($user_info->roles
). have tested against own , works well.
see below use of array_intersect
.
<?php global $user_login, $current_user; get_currentuserinfo(); $user_info = get_userdata($current_user->id); $roles = array ( 'administrator', 'subscriber', ); if (is_user_logged_in() && array_intersect( $roles, $user_info->roles)) { echo 'success'; } else { echo 'failure'; } ?>
example 1: link $roles
array not match.
example 2: link $roles
array has 1 match.
Comments
Post a Comment