Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Last active January 20, 2016 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dingo-d/089dc34f7cc88eca5a19 to your computer and use it in GitHub Desktop.
Save dingo-d/089dc34f7cc88eca5a19 to your computer and use it in GitHub Desktop.
A part of the functions.php file that is used to log in or register user via AJAX. Part of Header AJAX login/register gist (2)
<?php
/********* AJAX Login ***********/
function yourtheme_ajax_login_init(){
wp_register_script('ajax-login-script', get_template_directory_uri() . '/js/ajax-login-script.js', array('jquery') );
wp_enqueue_script('ajax-login-script');
wp_localize_script( 'ajax-login-script', 'ajax_login_object', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'redirecturl' => home_url(),
'loadingmessage' => esc_html__('Sending user info, please wait...', 'yourtheme')
));
// Enable the user with no privileges to run ajax_login() in AJAX
add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
}
// Execute the action only if the user isn't logged in
if (!is_user_logged_in()) {
add_action('init', 'yourtheme_ajax_login_init');
}
if (!function_exists('ajax_login')) {
function ajax_login(){
// First check the nonce, if it fails the function will break
check_ajax_referer( 'ajax-login-nonce', 'security' );
// Nonce is checked, get the POST data and sign user on
$info = array();
$info['user_login'] = $_POST['username'];
$info['user_password'] = $_POST['password'];
$info['remember'] = true;
$user_signon = wp_signon( $info, false );
if ( is_wp_error($user_signon) ){
echo json_encode(array('loggedin'=>false, 'message'=> esc_html__('Wrong username or password.', 'yourtheme')));
} else {
echo json_encode(array('loggedin'=>true, 'message'=> esc_html__('Login successful, redirecting...', 'yourtheme')));
}
die();
}
}
/********* AJAX Registration ***********/
add_action( 'wp_ajax_register_action', 'yourtheme_handle_registration' );
add_action( 'wp_ajax_nopriv_register_action', 'yourtheme_handle_registration' );
if (!function_exists('yourtheme_handle_registration')) {
function yourtheme_handle_registration(){
if( $_POST['action'] == 'register_action' ) {
$error = '';
$uname = trim( $_POST['username'] );
$email = trim( $_POST['mail_id'] );
$fname = trim( $_POST['firname'] );
$lname = trim( $_POST['lasname'] );
if( empty( $_POST['username'] ) )
$error .= '<p class="error">'.esc_html__('Enter username', 'yourtheme').'</p>';
if( empty( $_POST['mail_id'] ) )
$error .= '<p class="error">'.esc_html_('Enter email', 'yourtheme').'</p>';
elseif( !filter_var($email, FILTER_VALIDATE_EMAIL) )
$error .= '<p class="error">'.esc_html__('Enter valid email', 'yourtheme').'</p>';
if( empty( $_POST['firname'] ) )
$error .= '<p class="error">'.esc_html__('Enter first name', 'yourtheme').'</p>';
elseif( !preg_match("/^[a-zA-Z'-]+$/",$fname) )
$error .= '<p class="error">'.esc_html__('Enter valid first name', 'yourtheme').'</p>';
if( empty( $_POST['lasname'] ) )
$error .= '<p class="error">'.esc_html__('Enter last name', 'yourtheme').'</p>';
elseif( !preg_match("/^[a-zA-Z'-]+$/",$lname) )
$error .= '<p class="error">'.esc_html__('Enter valid last name', 'yourtheme').'</p>';
if( empty( $error ) ){
$status = wp_create_user( $uname, $email );
if( is_wp_error($status) ){
$msg = '';
foreach( $status->errors as $key=>$val ){
foreach( $val as $k=>$v ){
$msg = '<p class="error">'.$v.'</p>';
}
}
echo $msg;
} else{
$msg = '<p class="success">'.esc_html__('Registration successful!', 'yourtheme').'</p>';
echo $msg;
}
} else{
echo $error;
}
die(1);
}
}
}
add_action( 'user_register', 'yourtheme_register_user_metadata' );
add_action( 'profile_update', 'yourtheme_register_user_metadata' );
if (!function_exists('yourtheme_register_user_metadata')) {
function yourtheme_register_user_metadata( $user_id ){
if( !empty( $_POST['firname'] ) && !empty( $_POST['lasname'] ) && !empty( $_POST['username'] ) && !empty( $_POST['mail_id'] ) ){
update_user_meta( $user_id, 'first_name', trim($_POST['firname']) );
update_user_meta( $user_id, 'last_name', trim($_POST['lasname']) );
update_user_meta( $user_id, 'username', trim($_POST['username']) );
update_user_meta( $user_id, 'mail_id', trim($_POST['mail_id']) );
}
update_user_meta( $user_id, 'show_admin_bar_front', false );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment