update_meta_user difference in i:1 and b:1, how to write b:1

This code leads to wp_cababilities being written to the database but with an i:1 in front.

if( $new_roles === 'administrator' ) {
    $capabilities = array ("administrator",'activate_plugins','delete_others_pages','delete_others_posts','delete_pages','delete_plugins','delete_posts','delete_private_pages','delete_private_posts','delete_published_pages','delete_published_posts','edit_dashboard','edit_files','edit_others_pages','edit_others_posts','edit_pages','edit_posts','edit_private_pages','edit_private_posts','edit_published_pages','edit_published_posts','edit_theme_options','export','import','list_users','manage_categories','manage_links','manage_options','moderate_comments','promote_users','publish_pages','publish_posts','read_private_pages','read_private_posts','read','remove_users','switch_themes','upload_files','create_product');
    $user->set_role('administrator');
    update_user_meta( $user_id, 'wp_capabilities', $capabilities );
    update_user_meta( $user_id, 'wp_user_level', 10 );
}

This is what is placed in the database:

Read More
a:39:i:0;s:13:"administrator";i:1;s:16:"activate_plugins";i:2;s:19:"delete_others_pages";i:3;s:19:"delete_others_posts";i:4;s:12:"delete_pages";i:5;s:14:"delete_plugins";i:6;s:12:"delete_posts";i:7;s:20:"delete_private_pages";i:8;s:20:"delete_private_posts";i:9;s:22:"delete_published_pages";i:10;s:22:"delete_published_posts";i:11;s:14:"edit_dashboard";i:12;s:10:"edit_files";i:13;s:17:"edit_others_pages";i:14;s:17:"edit_others_posts";i:15;s:10:"edit_pages";i:16;s:10:"edit_posts";i:17;s:18:"edit_private_pages";i:18;s:18:"edit_private_posts";i:19;s:20:"edit_published_pages";i:20;s:20:"edit_published_posts";i:21;s:18:"edit_theme_options";i:22;s:6:"export";i:23;s:6:"import";i:24;s:10:"list_users";i:25;s:17:"manage_categories";i:26;s:12:"manage_links";i:27;s:14:"manage_options";i:28;s:17:"moderate_comments";i:29;s:13:"promote_users";i:30;s:13:"publish_pages";i:31;s:13:"publish_posts";i:32;s:18:"read_private_pages";i:33;s:18:"read_private_posts";i:34;s:4:"read";i:35;s:12:"remove_users";i:36;s:13:"switch_themes";i:37;s:12:"upload_files";i:38;s:14:"create_product";}

These capabilities do not work for that user.

A simple copy/paste of the following into the database gives the user the capabilities

a:27:{s:13:"administrator";b:1;s:12:"delete_posts";b:1;s:22:"delete_published_posts";b:1;s:10:"edit_posts";b:1;s:20:"edit_published_posts";b:1;s:13:"publish_posts";b:1;s:4:"read";b:1;s:12:"upload_files";b:1;s:19:"delete_others_pages";b:1;s:19:"delete_others_posts";b:1;s:12:"delete_pages";b:1;s:11:"delete_post";b:1;s:20:"delete_private_pages";b:1;s:20:"delete_private_posts";b:1;s:22:"delete_published_pages";b:1;s:17:"edit_others_pages";b:1;s:17:"edit_others_posts";b:1;s:10:"edit_pages";b:1;s:18:"edit_private_pages";b:1;s:18:"edit_private_posts";b:1;s:20:"edit_published_pages";b:1;s:17:"manage_categories";b:1;s:12:"manage_links";b:1;s:17:"moderate_comments";b:1;s:13:"publish_pages";b:1;s:18:"read_private_pages";b:1;s:18:"read_private_posts";b:1;}

What is the difference between the i:1 and b:1 ? How can I get the update_meta_user to add the b:1?

I cannot find a document but curious if this is i for integer and b for boolean. In which case, how can I get this code to update the wp_cabailities database field?

Related posts

1 comment

  1. Your first serialized string is broken. I can’t spot why but you should not be trying to manipulate capabilities manually. You already have a user object. Use it.

    if( $new_roles === 'administrator' ) {
      $capabilities = array ("administrator",'activate_plugins','delete_others_pages','delete_others_posts','delete_pages','delete_plugins','delete_posts','delete_private_pages','delete_private_posts','delete_published_pages','delete_published_posts','edit_dashboard','edit_files','edit_others_pages','edit_others_posts','edit_pages','edit_posts','edit_private_pages','edit_private_posts','edit_published_pages','edit_published_posts','edit_theme_options','export','import','list_users','manage_categories','manage_links','manage_options','moderate_comments','promote_users','publish_pages','publish_posts','read_private_pages','read_private_posts','read','remove_users','switch_themes','upload_files','create_product');
      $user->set_role('administrator');
      foreach ($capabilities as $cap) {
        $user->add_cap($cap);
      }
    }
    

    Untested but that should work. However, you should only need to explicitly add the capabilities not already implied by the role. You could make that capability array significantly shorter.

Comments are closed.