update_meta_user difference in i:1 and b:1, how to write b: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.

Leave a Comment