I am new to wordpress, in table wp_usermeta
I notice that we have a row
meta_key meta_value
wp_capabilities a:1:{s:13:"administrator";b:1;}
First, what’s the meaning of meta_value a:1:{s:13:"administrator";b:1;}
, where can I found the exactly explanation of this filed ? Or I what to know all about the role
user
Capabilities
, where can I get these information.
Second, how can I create new roles via wordpress's API
by code
Thanks in advanced.
The wp_capabilities saves the value as serilized array, you can try it in your php or for this example here: http://blog.tanist.co.uk/files/unserialize/.
The Code:
Is:
Meaning the user is an administrator.
You can add new roles to the database by running the function add_role, and only run it once!
In addition to @Krysiek’s answer, you should know that the data stored in the meta tables, including user metadata, is often serialized. If you’d like to know what the data actually represents, you can use the PHP function unserialize to determine it’s value. For example, running the value
a:1:{s:13:"administrator";b:1;}
through unserialize (and then var_dump-ing the results) products this:You will find the same sorts of entries in the
options
table as well as thepost_meta
table.However, you should avoid writing to these tables directly. There are WordPress functions that allow you to store and access data about users. For example, to read data from the user meta table, you should use get_user_meta and to write you should use the WordPress function update_user_meta. Similar functions exist for post_meta and options tables.
meta_value
is column to store value of meta field. Each meta_field contains key and value.I think this Codex article about roles and capabilities should help you:
http://codex.wordpress.org/Roles_and_Capabilities
To add custom roles you should use
add_role
function (http://codex.wordpress.org/Function_Reference/add_role)And sample of using it from Codex:
The key “wp_capabilities” in table wp_usermeta identifies the serialized representation of an array that identifies a user’s roles and any capabilities that have been added to that user with
$user->add_cap('my_capability');
. Thanks spaul!The WordPress team named the field [prefix]_capabilities before they realized that “capabilities” were actually things grouped together for users filling a particular role. Changing the value of the key from [prefix]_capabilities to [prefix]_roles would disrupt way too many third party plugins as well as requiring a huge refactoring in WordPress itself.
For this reason, it’s safe to say that the wp_capabilities usermeta key holds user roles and would be named wp_roles if there weren’t already so many useful pieces of software that expect it to use the old name.
Because this fact is so useful to new WordPress enthusiasts, I recommend that everyone who has anything to say about WordPress Roles (or capabilities) be very up front about the fact that user roles are listed in an array that is serialized in the usermeta table under the [prefix]_capabilities key.
In
wp_usermeta
table we can save the extra information related to a user while we creating a new user or we update a profile of existing user.In
wp_usermeta
table there is a column namemeta_key
where we can save the value through which we canget
orupdate
the value of that field for exampleadd_user_meta( 'user_id', 'meta_key', 'meta_value')
using this we can add the meta vale in the table havingmeta_key='meta_key'
.get_user_meta('user_id', 'meta_key', true)
by using this function we can get the value of that user havingmeta_key = 'meta_key'
.update_user_meta( 'user_id, 'meta_key', 'meta_value')
using this function we can update the meta value.delete_user_meta( 'user_id', 'meta_key')
using this we can delete the meta value of user.If you want the save the
Middle name
field for users then save it inwp_usermata
havingmeta_key=middle_name
and save it value inmeta_value=value
using these functions.Role is like access policy given to a user that he has only that much of access in the website.You can add, remove a role.
Capabilities is like adding features or restricting a role. For example there are 2 role ‘gold-role’, ‘platinum-role’. We are restrict ‘gold-role’ that he can onlu add post but not publish the post but to ‘platinum-role’ we adding a feature that he can publish a post. We can also add or remove capability related to a role. You can also add restriction on metaboxes of posts.
For more information follow Role and Capabilities
You can add a role and set its capabilities via wordpress API or You can use plugins to do this. In my opinion Advanced Access Manager is best plugin for handling of Role and Capabilities.