Hi,
I’ve been trying to get code working for a few hours now and can only manage one of two results. The code is Wordpress based but the issue is pure PHP.
Basically I’m trying to find a users Wordpress role. I’m achieving this by storing a meta_value in a string and then checking the string for the name of the role. This works perfectly in practice, however I’m trying to make it more dynamic but I can only do it manually or get the end of the array.
Allow me to explain.
[php]
function set_status() {
global $wp_roles;
global $wpdb;
$roles_list = $wp_roles->role_names;
$id = get_logged_in_user_id();
$query = $wpdb->get_var("SELECT wp_usermeta.meta_value FROM $wpdb->usermeta WHERE user_id = $id AND wp_usermeta.meta_key = 'wp_capabilities'");
$role = find_role($query, $roles_list);
}
//Returns the current user ID
function get_logged_in_user_id() {
global $current_user; //Include the global $current_user variable
get_currentuserinfo(); //Populate the $current_user variable with the logged in users info
if(is_user_logged_in()){ //If the user is logged in
if(is_object($current_user)){ //Check the $current_user variable is populated
$current_user_id = $current_user->ID; //Retrieve the current users ID
}
}
return $current_user_id; //Return the current user ID
}
function find_role($in_me, $find) {
foreach($find as $role){
$role = strtolower($role);
(strpos($in_me, $role) !== false);
continue;
}
return $role;
}
[/php]
What’s happening here is the first function is calling the second function to find the user id of the currently logged in user, then using that user id to find the correct database entry. The database entry looks like this:
a:1:{s:7:"patient";s:1:"1";}
As you can see “patient” is the currently logged in users role.
The get this from the string I’m comparing the string using strpos. This works perfectly like this:
[php]
$role = (strpos($query, “patient”) !== false);
[/php]
This will return true, so I can then say if it’s true return “patient”.
What I am trying to achieve with the third function is to pass the list of roles from the “$wp_roles->role_names” call as the second parameter and then loop through them all until it finds a match, then return that match.
$wp_roles->role_names returns this:
array(16) { ["administrator"]=> string(13) "Administrator" ["editor"]=> string(6) "Editor" ["author"]=> string(6) "Author" ["contributor"]=> string(11) "Contributor" ["subscriber"]=> string(10) "Subscriber" ["patient"]=> string(7) "Patient" ["care_home_user"]=> string(14) "Care_Home_User" ["care_home_admin"]=> string(15) "Care_Home_Admin" ["gp"]=> string(2) "GP" ["doctors_chemist_staff"]=> string(21) "Doctors_Chemist_Staff" ["pending"]=> string(7) "Pending" ["s2member_level1"]=> string(16) "s2Member Level 1" ["s2member_level2"]=> string(16) "s2Member Level 2" ["s2member_level3"]=> string(16) "s2Member Level 3" ["s2member_level4"]=> string(16) "s2Member Level 4" ["customer"]=> string(8) "Customer" }
But the for each loop I’ve got just returns the last item in the array every time. Breaking results in only the first item, I’ve tried do, while and simply while and I just cannot get the syntax right. I’m sure it’s me and I know this works in theory. In case it’s not clear the sudo code would read something like:
Does $role appear in the string “$query”?
If not try the next role in list.
If it matches exit loop and return matched value.
I hope that makes it clear.
Can someone please straighten this out for me, it’s driving me nuts as I’m not very good with loops.
Thanks in advance.