how to add custom user capabilities using add_user_meta or something else?

i have two WordPress sites with SSO Configurations as described here but the plugin in the answer didn’t work for me so i tried to write a code to add capabilities for second site users:

$wp_user_query = new WP_User_Query(array('role' => 'author'));
$users = $wp_user_query->get_results();
if (!empty($users)) {
    foreach ($users as $user)
    {
        add_user_meta( $user->id, 'orewpst_capabilities', "a:1:{s:6:'author';b:1;}", true ); 
        add_user_meta( $user->id, 'orewpst_user_level', '2', true ); 
    }
}

the problem is the output result for "a:1:{s:6:'author';b:1;}" is s:23:"a:1:{s:6:'author';b:1;}"

i don’t know what “s:23” means and why it appears in database!

update: I want the string "a:1:{s:6:'author';b:1;}" to store in database with out any change! but somehow my code adds an “s:23:” before it.

2 Answers
2

Try passing the value without serializing it manually, because WordPress will do it for you anyway:

add_user_meta( $user->id, 'orewpst_capabilities', array( 'author' => 1 ), true );

or

update_user_meta( $user->id, 'orewpst_capabilities', array( 'author' => 1 ) ); // it will create the meta data for you if it doesn't exist already.

The s:23 means that you stored a string with 23 chars.

Hope it helps!

Leave a Comment