Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom user registration via AJAX
<?php
/**
* Process user registration via AJAX
*
* A custom user registration process that listens for a
* specific type POST request that will create a new user
* on the blog.
*
* @action wp_ajax_nopriv_fjarrett_user_registration
*
* @return void
*/
function fjarrett_user_registration_ajax_callback() {
if (
'POST' !== $_SERVER['REQUEST_METHOD']
||
! isset( $_POST['registration_token'] )
||
! isset( $_POST['registration_nonce'] )
||
false === wp_verify_nonce( $_POST['registration_nonce'], 'user_registration-' . $_POST['registration_token'] )
||
! isset( $_POST['user_login'] )
||
! isset( $_POST['user_pass'] )
||
! isset( $_POST['first_name'] )
||
! isset( $_POST['last_name'] )
||
! isset( $_POST['user_email'] )
) {
wp_send_json_error( 'Doing it wrong.' );
}
if ( is_user_logged_in() ) {
wp_send_json_error( 'Sorry, you cannot register because you are already logged in.' );
}
$user_login = sanitize_user( filter_input( INPUT_POST, 'user_login' ) );
$user_pass = filter_input( INPUT_POST, 'user_pass' ); // Unsanitized
$first_name = sanitize_text_field( filter_input( INPUT_POST, 'first_name' ) );
$last_name = sanitize_text_field( filter_input( INPUT_POST, 'last_name' ) );
$user_email = sanitize_email( filter_input( INPUT_POST, 'user_email' ) );
if (
empty( $user_login )
||
empty( $first_name )
||
empty( $last_name )
||
empty( $user_email )
||
empty( $user_pass )
) {
wp_send_json_error( 'Some required fields were missing, please try again.' );
}
$user_id = wp_create_user( $user_login, $user_pass, $user_email );
if ( is_wp_error( $user_id ) ) {
wp_send_json_error( $user_id->get_error_message() );
}
$user_args = array(
'ID' => $user_id,
'first_name' => $first_name,
'last_name' => $last_name,
'display_name' => trim( sprintf( '%s %s', $first_name, $last_name ) ),
);
$user_id = wp_update_user( $user_args );
if ( is_wp_error( $user_id ) ) {
wp_send_json_error( $user_id->get_error_message() );
}
$creds = array(
'user_login' => $user_login,
'user_password' => $user_pass,
'remember' => true,
);
$user = wp_signon( $creds, false );
if ( is_wp_error( $user ) ) {
wp_send_json_error( $user->get_error_message() );
}
wp_send_json_success( home_url() );
}
add_action( 'wp_ajax_fjarrett_user_registration', 'fjarrett_user_registration_ajax_callback' );
add_action( 'wp_ajax_nopriv_fjarrett_user_registration', 'fjarrett_user_registration_ajax_callback' );
/**
* Enqueue scripts
*
* Load jQuery and make admin-ajax.php URL accessible
* on the front-end.
*
* @action wp_enqueue_scripts
*
* @return void
*/
function fjarrett_enqueue_scripts() {
wp_enqueue_script( 'jquery' );
wp_localize_script(
'jquery',
'fjarrett',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'fjarrett_enqueue_scripts' );
@fjarrett

This comment has been minimized.

Copy link
Owner Author

@fjarrett fjarrett commented Dec 18, 2014

@fjarrett

This comment has been minimized.

Copy link
Owner Author

@fjarrett fjarrett commented Dec 18, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment