Snip
|
For plugin authors, an API is available for retrieving a...
|
---|
Categories |
|
---|
For Snip |
loading snip actions ... |
---|---|
For Page |
loading url actions ... |
For plugin authors, an API is available for retrieving and manipulating roles and capabilities. A plugin can create a new capability called ‘do_foo’ and give that capability to the Adminstrator role with the following code.
$role = get_role('administrator');
$role->add_cap('do_foo');
To check if the currently logged in user has this capability, plugins can make use of the current_user_can() function.
if ( current_user_can('do_foo') ) ...
To check the capabilities for a certain user ID, instantiate a WP_User object.
$user_id = 1;
$user = new WP_User($user_id);
if ( $user->has_cap('do_foo') ) ...
If a brand new role is needed, one can be created using add_role() and add_cap().
$role = add_role('foo_doer', 'Foo Doer');
$role->add_cap('do_foo');
$role->add_cap('do_bar');
HTML |
<p>For plugin authors, an <a href="http://trac.wordpress.org/file/trunk/wp-includes/capabilities.php" onclick="javascript:pageTracker._trackPageview('/outbound/article/trac.wordpress.org');">API</a> is available for retrieving and manipulating roles and capabilities. A plugin can create a new capability called ‘do_foo’ and give that capability to the Adminstrator role with the following code.<br> <code><br> $role = get_role('administrator');</code></p> <p><code>$role->add_cap('do_foo');</code></p> <p>To check if the currently logged in user has this capability, plugins can make use of the current_user_can() function.</p> <p><code>if ( current_user_can('do_foo') ) ...</code></p> <p>To check the capabilities for a certain user ID, instantiate a WP_User object.<br> <code>$user_id = 1;</code></p> <p><code>$user = new WP_User($user_id);</code></p> <p><code>if ( $user->has_cap('do_foo') ) ...</code></p> <p>If a brand new role is needed, one can be created using add_role() and add_cap().<br> <code>$role = add_role('foo_doer', 'Foo Doer');</code></p> <p><code>$role->add_cap('do_foo');</code></p> <p><code>$role->add_cap('do_bar');</code></p> |
---|