Last active
March 10, 2017 10:39
-
-
Save stevenkellow/0849280fd5cc5942ebc64aef6bde2d01 to your computer and use it in GitHub Desktop.
Ninja Forms user create
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Create user after form has been sanitised | |
add_action('ninja_forms_post_process', 'nf_create_user'); | |
function nf_create_user(){ | |
// Call in Ninja Forms | |
global $ninja_forms_processing; | |
// Get the form ID | |
$form_id = $ninja_forms_processing->get_form_ID(); | |
// If the form ID is the one we want - change the number here depending on form used | |
if ( $form_id == 6 ){ | |
// Set up variables based on form values - change the numbers below based on field IDs | |
$first_name = $ninja_forms_processing->get_field_value( 7 ); | |
$surname = $ninja_forms_processing->get_field_value( 8 ); | |
$u_email = $ninja_forms_processing->get_field_value( 9 ); | |
$username = $ninja_forms_processing->get_field_value( 10 ); | |
$password = $ninja_forms_processing->get_field_value( 11 ); | |
// Create an array of the user data - check out the available user fields for left hand side here: https://codex.wordpress.org/Function_Reference/wp_insert_user | |
$userdata = array( | |
'first_name' => $first_name, | |
'last_name' => $surname, | |
'user_login' => $username, | |
'user_email' => $u_email, | |
'user_pass' => $password, | |
'role' => 'subscriber' | |
); | |
// Create the user and remember the user id | |
$user_id = wp_insert_user( $userdata ) ; | |
// Use the user id to log in straight away | |
wp_set_auth_cookie( $user_id, true ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Create user after form has been sanitised | |
add_action( 'ninja_forms_after_submission', 'nf_create_new_user' ); | |
function nf_create_new_user( $form_data ){ | |
$form_id = $form_data[ 'form_id' ]; | |
// If form ID is the one we want | |
if( $form_id == 6 ){ | |
// Load up the fields we have for this form | |
$form_fields = $form_data[ 'fields' ]; | |
// Set up variables based on form values - change the numbers below based on field IDs | |
$first_name = $form_fields[ 7 ][ 'value' ]; | |
$surname = $form_fields[ 8 ][ 'value' ]; | |
$u_email = $form_fields[ 9 ][ 'value' ]; | |
$username = $form_fields[ 10 ][ 'value' ]; | |
$password = $form_fields[ 11 ][ 'value' ]; | |
// Create an array of the user data - check out the available user fields for left hand side here: https://codex.wordpress.org/Function_Reference/wp_insert_user | |
$userdata = array( | |
'first_name' => $first_name, | |
'last_name' => $surname, | |
'user_login' => $username, | |
'user_email' => $u_email, | |
'user_pass' => $password, | |
'role' => 'subscriber' | |
); | |
// Create the user and remember the user id | |
$user_id = wp_insert_user( $userdata ) ; | |
// Use the user id to log in straight away | |
wp_set_auth_cookie( $user_id, true ); | |
} | |
} |
I also had to change how I was retrieving the form field values:
$first_name = $form_fields[ "7" ]["value"];
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It looks like line 5 of the 3.x snippet should check for 'form_id' instead of 'id'.
$form_id = $form_data[ 'form_id' ];