Batch Create - Add user meta, first name, last name, etc..

Is there any way to add user meta, and other user information for a user using Batch Create plugin? I have ~750 users to add to a site and I have their full names and would like to prepopulate these. Thank you.

  • Vaughan

    Hi @maxaud,

    Hope you're well?

    I don't think this is possible without custom coding the plugin.

    I've taken a quick look but i'm not sure exactly on how this can be done.

    From initial looks, you may need to edit the model.php

    /batch-create/model/model.php

    If you edit that file, you will see the following;

    private function create_queue_table() {
    		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    
    		$sql = "CREATE TABLE $this->queue (
    				  batch_create_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    				  batch_create_site bigint(20) DEFAULT NULL,
    				  batch_create_blog_name varchar(255) NOT NULL DEFAULT 'null',
    				  batch_create_blog_title varchar(255) NOT NULL DEFAULT 'null',
    				  batch_create_user_name varchar(255) NOT NULL DEFAULT 'null',
    				  batch_create_user_pass varchar(255) NOT NULL DEFAULT 'null',
    				  batch_create_user_email varchar(255) NOT NULL DEFAULT 'null',
    				  batch_create_user_role varchar(255) NOT NULL DEFAULT 'null',
    				  PRIMARY KEY  (batch_create_ID)
    				) ENGINE=InnoDB $this->db_charset_collate;";
    
            dbDelta($sql);
    	}

    So you will need to add the extra columns there. for example; if we add the display_name column.

    private function create_queue_table() {
    		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    
    		$sql = "CREATE TABLE $this->queue (
    				  batch_create_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    				  batch_create_site bigint(20) DEFAULT NULL,
    				  batch_create_blog_name varchar(255) NOT NULL DEFAULT 'null',
    				  batch_create_blog_title varchar(255) NOT NULL DEFAULT 'null',
    				  batch_create_user_name varchar(255) NOT NULL DEFAULT 'null',
    				  batch_create_user_pass varchar(255) NOT NULL DEFAULT 'null',
    				  batch_create_user_email varchar(255) NOT NULL DEFAULT 'null',
    				  batch_create_user_role varchar(255) NOT NULL DEFAULT 'null',
              batch_create_display_name varchar(255) NOT NULL DEFAULT 'null',
    				  PRIMARY KEY  (batch_create_ID)
    				) ENGINE=InnoDB $this->db_charset_collate;";
    
            dbDelta($sql);
    	}

    Now we need to also add this to the insert_queue function

    function insert_queue( $args ) {
    		global $wpdb, $current_site;
    
    		array_unshift( $args, $current_site->id );
    
    		$args[2] = iconv( "Windows-1252", "UTF-8", $args[2] );
    
    		$wpdb->insert(
    			$this->queue,
    			array(
    				'batch_create_site'			=> $args[0],
    				'batch_create_blog_name'	=> $args[1],
    				'batch_create_blog_title'	=> $args[2],
    				'batch_create_user_name'	=> $args[3],
    				'batch_create_user_pass'	=> $args[4],
    				'batch_create_user_email'	=> $args[5],
    				'batch_create_user_role'	=> $args[6],
    			),
    			array(  '%d', '%s', '%s', '%s', '%s', '%s', '%s' )
    		);
    
    		return $wpdb->insert_id;
    
    	}

    so we add the display_name to this too

    function insert_queue( $args ) {
    		global $wpdb, $current_site;
    
    		array_unshift( $args, $current_site->id );
    
    		$args[2] = iconv( "Windows-1252", "UTF-8", $args[2] );
    
    		$wpdb->insert(
    			$this->queue,
    			array(
    				'batch_create_site'			=> $args[0],
    				'batch_create_blog_name'	=> $args[1],
    				'batch_create_blog_title'	=> $args[2],
    				'batch_create_user_name'	=> $args[3],
    				'batch_create_user_pass'	=> $args[4],
    				'batch_create_user_email'	=> $args[5],
    				'batch_create_user_role'	=> $args[6],
            'batch_create_display_name'	=> $args[7],
    			),
    			array(  '%d', '%s', '%s', '%s', '%s', '%s', '%s' , '%s')
    		);
    
    		return $wpdb->insert_id;
    
    	}

    notice the extra %s in the array at bottom. %s = a string. if you expect or it should only be a numeric value, then you should use %d.

    So hopefully with those changes.

    Now you should be able to add display_name to the csv file in the next column after the user role.

    I have no idea if these are the only changes required, so it might not work as is. I would recommend testing this on a test site first, or create a full backup of the DB beforehand, so you can restore the DB if something goes wrong.

    I'll also ask one of our developers to see if this method is correct & he should be able to confirm if other changes would be needed.

    Hope this helps

  • Jose

    Hi there @maxaud,

    Hope you are doing great today,

    I went ahead and added this feature to a beta version that I'm attaching here.

    Vaughan's approach was correct, but it required severeal other changes as well. Including the metadata parsing and the logic to trigger the DB schema update.

    Please notice that installing this version will re-create the schema, removing every pending imports in the queue -if there is any- .

    For the data input format, I followed your sugestion: It is a pipe delimited string in a single column.
    I used double pipe to delimit each metadata item, and a single pipe to delimit meta_key and meta_value.
    So, the format would be meta_key1|meta_value1||meta_key2|meta_value2

    You can also import serialized values if you want. This should work jsut fine:
    first_name|John||last_name|Smith||_sample_serialized_meta|a:1:{s:13:"administrator";b:1;}

    I ran my tests using a csv file, but it should work with an xsl as well..

    I'm attaching a screenshot of my csv as an example.

    Please let me know how it works for you.

    Cheers,
    Jose

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.