{"id":15,"date":"2019-11-06T21:05:53","date_gmt":"2019-11-07T03:05:53","guid":{"rendered":"https:\/\/travisolson.net\/code\/?p=15"},"modified":"2019-11-07T10:52:16","modified_gmt":"2019-11-07T16:52:16","slug":"wordpress-check-if-a-user-has-a-specific-role","status":"publish","type":"post","link":"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/","title":{"rendered":"WordPress: Check if a User Has a Specific Role"},"content":{"rendered":"<p>The default current_user_can() and user_can() functions are adequate for checking if a user belongs to a role, but I find the function names confusing.\u00a0 Also, those functions check both roles and capabilities, which seems inefficient if one only needs to check the role.<\/p>\n<p>This function does one thing only: check if a user belongs to a role.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">function myprefix_user_has_role($user, $rolename) {\r\n  if ( !is_a($user, 'WP_User') ) {\r\n    $user = get_userdata( $user );\r\n  }\r\n  if ( !$user ) {\r\n    return false;\r\n  }\r\n\r\n  foreach($user-&gt;roles as $role) {\r\n    if (strtolower($rolename) === strtolower($role) ) {\r\n      return true;\r\n    }\r\n  }\r\n  \r\n  return false;\r\n}<\/pre>\n<p>A common usage is to check if a user an administrator, so this function works along with the above function to do just that:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">function myprefix_is_admin($user = null) {\r\n  if ($user == null) {\r\n    $user = get_current_user_id();\r\n  }\r\n  return myprefix_user_has_role($user, 'administrator');\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The default current_user_can() and user_can() functions are adequate for checking if a user belongs to a role, but I find the function names confusing.\u00a0 Also, those functions check both roles and capabilities, which seems inefficient if one only needs to check the role. This function does one thing only: check if a user belongs to&#8230;  <a class=\"excerpt-read-more\" href=\"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/\" title=\"Read WordPress: Check if a User Has a Specific Role\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>WordPress: Check if a User Has a Specific Role - Travis Olson&#039;s Code Snippets<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WordPress: Check if a User Has a Specific Role - Travis Olson&#039;s Code Snippets\" \/>\n<meta property=\"og:description\" content=\"The default current_user_can() and user_can() functions are adequate for checking if a user belongs to a role, but I find the function names confusing.\u00a0 Also, those functions check both roles and capabilities, which seems inefficient if one only needs to check the role. This function does one thing only: check if a user belongs to... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/\" \/>\n<meta property=\"og:site_name\" content=\"Travis Olson&#039;s Code Snippets\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-07T03:05:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-07T16:52:16+00:00\" \/>\n<meta name=\"author\" content=\"Travis Olson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Travis Olson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/\"},\"author\":{\"name\":\"Travis Olson\",\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\"},\"headline\":\"WordPress: Check if a User Has a Specific Role\",\"datePublished\":\"2019-11-07T03:05:53+00:00\",\"dateModified\":\"2019-11-07T16:52:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/\"},\"wordCount\":94,\"publisher\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\"},\"articleSection\":[\"WordPress\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/\",\"url\":\"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/\",\"name\":\"WordPress: Check if a User Has a Specific Role - Travis Olson&#039;s Code Snippets\",\"isPartOf\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#website\"},\"datePublished\":\"2019-11-07T03:05:53+00:00\",\"dateModified\":\"2019-11-07T16:52:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.travisolson.net\/code\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WordPress: Check if a User Has a Specific Role\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.travisolson.net\/code\/#website\",\"url\":\"https:\/\/www.travisolson.net\/code\/\",\"name\":\"Travis Olson&#039;s Code Snippets\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.travisolson.net\/code\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\",\"name\":\"Travis Olson\",\"logo\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.travisolson.net\/code\/author\/travis-olson\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WordPress: Check if a User Has a Specific Role - Travis Olson&#039;s Code Snippets","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/","og_locale":"en_US","og_type":"article","og_title":"WordPress: Check if a User Has a Specific Role - Travis Olson&#039;s Code Snippets","og_description":"The default current_user_can() and user_can() functions are adequate for checking if a user belongs to a role, but I find the function names confusing.\u00a0 Also, those functions check both roles and capabilities, which seems inefficient if one only needs to check the role. This function does one thing only: check if a user belongs to... Read more &raquo;","og_url":"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/","og_site_name":"Travis Olson&#039;s Code Snippets","article_published_time":"2019-11-07T03:05:53+00:00","article_modified_time":"2019-11-07T16:52:16+00:00","author":"Travis Olson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Travis Olson","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/#article","isPartOf":{"@id":"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/"},"author":{"name":"Travis Olson","@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934"},"headline":"WordPress: Check if a User Has a Specific Role","datePublished":"2019-11-07T03:05:53+00:00","dateModified":"2019-11-07T16:52:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/"},"wordCount":94,"publisher":{"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934"},"articleSection":["WordPress"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/","url":"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/","name":"WordPress: Check if a User Has a Specific Role - Travis Olson&#039;s Code Snippets","isPartOf":{"@id":"https:\/\/www.travisolson.net\/code\/#website"},"datePublished":"2019-11-07T03:05:53+00:00","dateModified":"2019-11-07T16:52:16+00:00","breadcrumb":{"@id":"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.travisolson.net\/code\/wordpress-check-if-a-user-has-a-specific-role\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.travisolson.net\/code\/"},{"@type":"ListItem","position":2,"name":"WordPress: Check if a User Has a Specific Role"}]},{"@type":"WebSite","@id":"https:\/\/www.travisolson.net\/code\/#website","url":"https:\/\/www.travisolson.net\/code\/","name":"Travis Olson&#039;s Code Snippets","description":"","publisher":{"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.travisolson.net\/code\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934","name":"Travis Olson","logo":{"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/image\/"},"url":"https:\/\/www.travisolson.net\/code\/author\/travis-olson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":2,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":26,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts\/15\/revisions\/26"}],"wp:attachment":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}