Skip to content

Instantly share code, notes, and snippets.

@stevenkellow
Last active March 10, 2017 10:39
Show Gist options
  • Save stevenkellow/0849280fd5cc5942ebc64aef6bde2d01 to your computer and use it in GitHub Desktop.
Save stevenkellow/0849280fd5cc5942ebc64aef6bde2d01 to your computer and use it in GitHub Desktop.
Ninja Forms user create
<?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 );
}
}
<?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 );
}
}
@mattcsloan
Copy link

It looks like line 5 of the 3.x snippet should check for 'form_id' instead of 'id'.

$form_id = $form_data[ 'form_id' ];

@mattcsloan
Copy link

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