Skip to content

Instantly share code, notes, and snippets.

@onliniak
Created November 25, 2019 21:56
Show Gist options
  • Save onliniak/7bdf96c520ccd13e180688243bcc16d8 to your computer and use it in GitHub Desktop.
Save onliniak/7bdf96c520ccd13e180688243bcc16d8 to your computer and use it in GitHub Desktop.
WordPress AJAX login/register
//jQuery edition
jQuery("#login_button").click(function(){
var email = document.getElementById("username").value;
var pass = document.getElementById("Pass").value;
var secu = document.getElementById("security").value;
jQuery.ajax({
type: 'POST',
url: 'login.php',
data: {username: email, password: pass, security: secu},
success: function(response) {
//window.location.replace("http://mywordpress.com");
},
});
});
jQuery("#register_button").click(function(){
var user = document.getElementById("username_regi").value;
var pass = document.getElementById("Pass_regi").value;
var email = document.getElementById("email_regi").value;
var secu = document.getElementById("security2").value;
jQuery.ajax({
type: 'POST',
url: 'register.php',
data: {username: user, email: email, password: pass, security: secu},
success: function(response) {
//window.location.replace("http://mywordpress.com");
},
});
});
<!-- All files borrowed from http://natko.com/wordpress-ajax-login-without-a-plugin-the-right-way/-->
<button class="accordion"><?php echo __( 'Zaloguj się:', 'gist' ) ?></button>
<div id="loginme" class="panel">
<input id="username" type="text" name="username" placeholder="<?php echo __( 'Użytkownik:', 'gist' ) ?>">
<input id="Pass" type="password" name="password" placeholder="<?php echo __( 'Hasło:', 'gist' ) ?>">
<br />
<a class="lost" href="<?php echo wp_lostpassword_url(); ?>"><?php echo __( 'Zapomniałeś/-aś swojego hasła ?', 'gist' ) ?></a>
<br />
<input id="login_button" type="submit" value="Login" name="submit">
<?php wp_nonce_field( 'ajax-login-nonce', 'security' ); ?>
</div>
<button class="accordion"><?php echo __( 'Zarejestruj się:', 'gist' ) ?></button>
<div class="panel">
<input id="username_regi" type="text" name="username" placeholder="<?php echo __( 'Użytkownik:', 'gist' ) ?>">
<input id="email_regi" type="email" name="username" placeholder="<?php echo __( 'E-mail:', 'gist' ) ?>">
<input id="Pass_regi" type="password" name="password" placeholder="<?php echo __( 'Hasło:', 'gist' ) ?>">
<br />
<input id="register_button" type="submit" value="Login" name="submit">
<?php wp_nonce_field( 'ajax-register-nonce', 'security2' ); ?>
</div>
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php';
// 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;
// login
$user_signon = wp_signon( $info, false );
die();
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php';
// Nonce not work with registration … in my case.
//check_ajax_referer( 'ajax-register-nonce', 'security2' );
// register
wp_create_user($_POST['username'], $_POST['password'], $_POST['email']);
// login
$info = array();
$info['user_login'] = $_POST['username'];
$info['user_password'] = $_POST['password'];
$info['remember'] = true;
// login
$user_signon = wp_signon( $info, false );
die();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment