<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>capabilites &#8211; Blackbams Blog</title>
	<atom:link href="https://blog.blackbam.at/tag/capabilites/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.blackbam.at</link>
	<description>development - digital arts - internet</description>
	<lastBuildDate>Wed, 05 Oct 2011 17:21:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
	<item>
		<title>Digging into roles and capabilities in WordPress &#8211; extension and global usage</title>
		<link>https://blog.blackbam.at/2011/07/02/digging-into-roles-and-capabilities-in-wordpress-extension-and-global-usage/</link>
					<comments>https://blog.blackbam.at/2011/07/02/digging-into-roles-and-capabilities-in-wordpress-extension-and-global-usage/#respond</comments>
		
		<dc:creator><![CDATA[Blackbam]]></dc:creator>
		<pubDate>Sat, 02 Jul 2011 15:12:52 +0000</pubDate>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP Scripts]]></category>
		<category><![CDATA[capabilites]]></category>
		<category><![CDATA[explanation]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[WordPress Plugins]]></category>
		<guid isPermaLink="false">https://blog.blackbam.at/?p=1409</guid>

					<description><![CDATA[You may have noticed that understanding and extending roles and capabilities in WordPress can be a bit confusing as a start. On the one hand there are the default roles: (Super Admin), Administrator, Editor, Author, Contributor, Subscriber, and they all have more or less rights (=capabilities) to do actions on a WordPress website, with descending [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>You may have noticed that understanding and extending <a href="http://codex.wordpress.org/Roles_and_Capabilities">roles and capabilities in WordPress</a> can be a bit confusing as a start. On the one hand there are the default roles: (Super Admin), Administrator, Editor, Author, Contributor, Subscriber, and they all have more or less rights (=capabilities) to do actions on a WordPress website, with descending rights as they are ordered here. </p>
<p>&nbsp;</p>
<p>These roles do not exist since the early days of WordPress. They are mapped onto the traditional WordPress role system, which is a user level system from 0-10, while 10 is the highest level (all rights) and 0 is the lowest level (fewest rights). This interrelationship is shown on the following table from the Codex:</p>
<p>&nbsp;</p>
<p><a href="https://blog.blackbam.at/wp-content/uploads/2011/07/wp_role_capability_level-600x327.jpg"><img fetchpriority="high" decoding="async" src="https://blog.blackbam.at/wp-content/uploads/2011/07/wp_role_capability_level-600x327.jpg" alt="" title="WP roles and capabilities interrelationship" width="600" height="327" class="alignnone size-medium wp-image-1515" srcset="https://blog.blackbam.at/wp-content/uploads/2011/07/wp_role_capability_level-600x327.jpg 600w, https://blog.blackbam.at/wp-content/uploads/2011/07/wp_role_capability_level.jpg 772w" sizes="(max-width: 600px) 100vw, 600px" /></a></p>
<p>&nbsp;</p>
<p>Each of the user levels has a certain amount of capabilities. These capabilities exactly define what a WordPress user is allowed to do on the website. The capability &#8220;edit_posts&#8221;, for example, allows a user to edit his own published posts, the capability &#8220;add_users&#8221; allows a user to add new users to the system, and so on.</p>
<p>&nbsp;</p>
<h2>Important things to know about roles and capabilities</h2>
<p>The default system may be enough for blog systems, but not when it comes to WordPress as a CMS. The following things are important to know:</p>
<p><p>&nbsp;</p>
<ul>
<li>It is possible to create custom roles and custom capabilities (as they are registered default with <a href="http://codex.wordpress.org/Post_Types", f.e. "edit_job")>custom post types</a>)</li>
<li>One user can have more than one of the default roles, a user could be an &#8220;administrator&#8221;, &#8220;editor&#8221; and something like a custom &#8220;job_publisher&#8221; at the same time, for example. These roles are stored in the user-meta table for every user, in the field Table Prefix + capabilities.</li>
<li>You can make a semantic rule, that users on your website only are allowed to have one role at the same time, if you want to have a simplified role handling</li>
</ul>
<h2>How to extend the default role system then?</h2>
<p>You can search for functions and information how to do that in the Function Reference or using Google. I recommend the great and well-known <a href="http://wordpress.org/extend/plugins/members/">Members Plugin by Justin Tadlock</a>, which will make managing these things quite easy using the WordPress backend. Just check it out. It is described as follows:</p>
<p>&nbsp;</p>
<p><em></p>
<p>&#8220;Members is a plugin that extends your control over your blog. It&#8217;s a user, role, and content management plugin that was created to make WordPress a more powerful CMS.</p>
<p>&nbsp;</p>
<p>The foundation of the plugin is its extensive role and capability management system. This is the backbone of all the current features and planned future features.&#8221;</p>
<p></em></p>
<p>&nbsp;</p>
<h2>How can we easily get roles and work with roles in Plugins and Themes now?</h2>
<p>The following functions proved to be really useful, as they work almost anywhere you call them inside WordPress.</p>
<p>&nbsp;</p>
<div class="code_title">Returns the current users&#8217; roles on every page inside WordPress as an array</div>
<pre lang="php">
function getUserRolesAdminEdit() {
	
	$current_user = wp_get_current_user();
	
	$currentuserid = $current_user->ID;
	
	if(isset($_REQUEST['user_id']) && $_REQUEST['user_id']!="") {
		$currentuserid = $_REQUEST['user_id'];
	}
	
	$userroles = array('subscriber');
	
	$user = new WP_User( $currentuserid );
	
	if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
		$userroles=$user->roles;
	}
	
	return $userroles;
}
</pre>
<p>&nbsp;</p>
<div class="code_title">&#8230;or the current users&#8217; role as a string, if you are sure there is only one per user</div>
<pre lang="php">
function getUserRoleAdminEdit() {
	
	$current_user = wp_get_current_user();
	
	$currentuserid = $current_user->ID;
	
	if(isset($_REQUEST['user_id']) && $_REQUEST['user_id']!="") {
		$currentuserid = $_REQUEST['user_id'];
	}
	
	$userrole = 'subscriber';
	
	$user = new WP_User( $currentuserid );
	
	if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
		// only change this if one user can have more than one role, which is not expected
		$userrole=$user->roles[0];
	}
	
	return $userrole;
}
</pre>
<p>&nbsp;</p>
<div class="code_title">The following function always returns the correct user ID, which can be useful especially for admin pages</div>
<pre lang="php">
function getUserIdAdminEdit() {
	
	$current_user = wp_get_current_user();
	
	$currentuserid = $current_user->ID;
	
	if(isset($_REQUEST['user_id']) && $_REQUEST['user_id']!="") {
		$currentuserid = $_REQUEST['user_id'];
	}
	
	return $currentuserid;
}
</pre>
<p><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.blackbam.at%2F2011%2F07%2F02%2Fdigging-into-roles-and-capabilities-in-wordpress-extension-and-global-usage%2F&#038;title=Digging%20into%20roles%20and%20capabilities%20in%20WordPress%20%E2%80%93%20extension%20and%20global%20usage" data-a2a-url="https://blog.blackbam.at/2011/07/02/digging-into-roles-and-capabilities-in-wordpress-extension-and-global-usage/" data-a2a-title="Digging into roles and capabilities in WordPress – extension and global usage"><img src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.blackbam.at/2011/07/02/digging-into-roles-and-capabilities-in-wordpress-extension-and-global-usage/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
