what’s the meaning of the field wp_capabilities in table wp_usermeta

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.

Read More

Second, how can I create new roles via wordpress's API by code

Thanks in advanced.

Related posts

5 comments

  1. 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:

    a:1:{s:13:"administrator";b:1;}
    

    Is:

    Array
    (
       [administrator] => 1
    )
    

    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!

  2. 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:

    array(1) {
        ["administrator"]=>
        bool(true)
    }
    

    You will find the same sorts of entries in the options table as well as the post_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.

  3. 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:

    $result = add_role('basic_contributor', 'Basic Contributor', array(
        'read' => true, // True allows that capability
        'edit_posts' => true,
        'delete_posts' => false, // Use false to explicitly deny
    ));
    if (null !== $result) {
        echo 'Yay!  New role created!';
    } else {
        echo 'Oh... the basic_contributor role already exists.';
    }
    
  4. 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.

  5. 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 name meta_key where we can save the value through which we can get or update the value of that field for example

    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.

Comments are closed.