Skip to content

Instantly share code, notes, and snippets.

@mikeselander
Forked from wpbandit/wp-check-user-role.php
Last active August 29, 2015 14:07
Show Gist options
  • Save mikeselander/66dff6f4af3c77ea7f7a to your computer and use it in GitHub Desktop.
Save mikeselander/66dff6f4af3c77ea7f7a to your computer and use it in GitHub Desktop.
<?php
function check_user_role($roles,$user_id=NULL) {
// Get user by ID, else get current user
if ($user_id)
$user = get_userdata($user_id);
else
$user = wp_get_current_user();
// No user found, return
if (empty($user))
return FALSE;
// Append administrator to roles, if necessary
if (!in_array('administrator',$roles))
$roles[] = 'administrator';
// Loop through user roles
foreach ($user->roles as $role) {
// Does user have role
if (in_array($role,$roles)) {
return TRUE;
}
}
// User not in roles
return FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment