• I need to add wp_capability to a set of users that I am adding to my wordpress site.

    I tried creating a column in the csv called wp_capability with the data in the format that wordpress stores capabilities. However, when I upload the csv, the plug-in does not update the user table and I get no error.

    How do I add a user capability field for the users I want to add to my wordpress site?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter hp3

    (@hp3)

    I created a column in my csv named ‘wp_capabilities’.

    I added the following capabilities string to the fields for the ‘wp_capabilities’ column.
    a:2:{s:10:”subscriber”;s:1:”1″;s:16:”post_edit_recipe”;s:1:”1″;}

    When I upload the csv, wordpress creates the user, creates the wp_capabilities field in the meta table but converts the above string to:
    s:64:”a:2:{s:10:”subscriber”;s:1:”1″;s:16:”post_edit_recipe”;s:1:”1″;}“;

    There is an s:64:” to the beginning of the string and “; at the end of the string. WordPress seems to ignore this capability formatting and the user does not have the capability function in the wordpress site.

    If I manually remove the s:64:” and “; then wordpress recognizes the capability values and the user has appropriate capabilities in the web site.

    Thread Starter hp3

    (@hp3)

    I altered the plug-in code to use the WP_user->add_cap method if there is a $metakey with the name “wp_capabilities”.

    I changed the original code at around line 148 in import-user-from-csv.php:

    foreach ( $usermeta as $metakey => $metavalue ) {
        update_user_meta( $user_id, $metakey, $metavalue );
    }

    to

    foreach ( $usermeta as $metakey => $metavalue ) {
        if($metakey == "wp_capabilities"){
            $special_user = new WP_User($user_id);
            $special_user->add_cap($metavalue);
        } else {
            update_user_meta( $user_id, $metakey, $metavalue );
        }
    }

    This calls the add_cap method so that WordPress properly saves the value in ‘wp_capabilities’.

    Thread Starter hp3

    (@hp3)

    Perhaps it would be more flexible if there was a way to detect if a field value in the csv contains a serialized version of data and then save the data to the meta table without WordPress re-serializing the data as a string.

    This would allow the csv to contain user meta parameters for plug-ins.

    Thread Starter hp3

    (@hp3)

    I might also be useful to edit the above code to check if a $metavalue contains content before creating a meta table field for it. This would prevent empty meta fields in the database.

    foreach ( $usermeta as $metakey => $metavalue ) {
        if(strlen($metavalue) > 0){
           if($metakey == "wp_capabilities"){
               $special_user = new WP_User($user_id);
               $special_user->add_cap($metavalue);
           } else {
               update_user_meta( $user_id, $metakey, $metavalue );
           }
        }
    }
    Thread Starter hp3

    (@hp3)

    One other challenge is adding a user to the database with a pre-existing user id. In my case I am adding a phpbb user database to my wordpress blog. I will be transfering some posts from phpbb as well as users and want to match up the posts with user ids.

    If a row in the csv has an id value that does not already exist in the wordpress user database, wordpress will generate error because the plug-in calls wp_update_user for a non existant user.

    I set the $update variable to false in line 134 to require the plug-in to use wp_insert_user instead of update user.

    It seems that if userdata passed to core wp_insert_user() contains an id then it will try to get the existing data for the user. In my case the user does not exist in the users table and the getuser() function returns false causing an error in other parts of wp_insert_user() which are expecting a user object.

    To insert a bunch of new users with user id intact, would require modifying the core wp_insert_user method to prevent the function from always calling getuser() if there is an id field.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Plugin: Import Users from CSV] adding wp_capability for users’ is closed to new replies.