Skip to content

Instantly share code, notes, and snippets.

@wpbandit
Last active October 14, 2016 08:11
Show Gist options
  • Save wpbandit/4707580 to your computer and use it in GitHub Desktop.
Save wpbandit/4707580 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