3

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.

1

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!

0

That's right the s:23 is serialized data. WordPress automatically store the data in serialized format.

Also, To set the user capabilities use function add_cap() instead of updating the user meta. For more details check WordPress handbook user roles and capabilities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.