It's not hard to do actually. Just add a post_submission function that uses the form entry id. All you need to know is the ID of the form you're using and the IDs of the fields you want to work with.
function create_wp_user($entry, $form){
if($form["id"] != 1)
return;
global $wpdb;
$id = $entry['id'];
//get the info. for the last form entry
$migrate = $wpdb->get_results("SELECT field_number, value FROM ".$wpdb->prefix."rg_lead_detail WHERE lead_id = $id ORDER BY field_number",'ARRAY_A');
//get the number of fields so we can loop through and process them
$count = count($migrate);
$input = array();
//here we move the results into a single level array of field number => field value
for($i=0;$i<$count;$i++){
$key = $migrate[$i]['field_number'];
$value =$migrate[$i]['value'];
$input[$key] = $value;
}
//now we have a single array, $input. Now create a WP user account. In this case, I made WP's user nicename and dsiplay name be a concatenation of their first and last name -- fields 3.3 and 3.6 on my example form. You might have separate fields for those.
$usernicename = $displayname = $input['3.3']." ".$input['3.6'];
$email = $input['5'];
$user_pass = wp_generate_password();
$curdate = date('Y-m-d-H-i-s');
$result= $wpdb->query($wpdb->prepare("INSERT INTO wp_users (user_login, user_pass, user_registered, user_email, user_nicename, display_name) VALUES (%s, %s, %s,%s, %s, %s)", array($email, $user_pass, $curdate , $email, $usernicename, $displayname) ) );
//and return the WP user ID of the member you just created
$user_id = $wpdb->insert_id;
//last thing is to fill in a few values in the member meta table. I'm making these guys be subscribers and since I'm using WordpressMU for this example, I'm also assigning a primary blog it -- which I guess WP3.0 uses too. And I was lazy, I hard coded the blog id. Should use WP function to insert the right one automatically
//now we add in the additonal user_meta values for the primary member //
$capabilities='a:1:{s:10:"subscriber";b:1;}';
$user_level ='10';
$result= $wpdb->insert("wp_usermeta", array('user_id' => $user_id, 'meta_key' => $wpdb->prefix.'capabilities', 'meta_value' => $capabilities) );
$result= $wpdb->insert("wp_usermeta", array('user_id' => $user_id, 'meta_key' => $wpdb->prefix.'user_level', 'meta_value' => $user_level) );
$result= $wpdb->insert("wp_usermeta", array('user_id' => $user_id, 'meta_key' => 'primary_blog', 'meta_value' => '2') );
$result= $wpdb->insert("wp_usermeta", array('user_id' => $user_id, 'meta_key' => 'first_name', 'meta_value' => $input['3.3']) );
$result= $wpdb->insert("wp_usermeta", array('user_id' => $user_id, 'meta_key' => 'last_name', 'meta_value' => $input['3.6']) );*/
} //end of the function
You can stick this whole thing in your theme functions file, then make it take effect by adding this right afterwards:
add_action("gform_post_submission", "create_wp_user", 10, 2);
If you want to have the password emailed to the user you can do that as well, I didn't bother in this case. The more likely scenario is you're letting them pick a password themselves so you'd simply get rid of the wp_generate_password() part and use the value they submitted.
Posted 14 years ago on Friday June 25, 2010 |
Permalink