Created
March 28, 2012 19:55
-
-
Save fengelz/2229969 to your computer and use it in GitHub Desktop.
Wordpress ajax sign in
This file contains 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
//Your frontend javascript | |
var data = { | |
action: 'sign_in', | |
username:"<username>", | |
pass:"<password>" | |
}; | |
jQuery.post('/wordpress/wp-admin/admin-ajax.php', data, | |
function(response) { | |
alert("signed in successfully"); | |
}); |
This file contains 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
//Your functions.php | |
add_action('wp_ajax_sign_in', 'ajax_sign_in'); | |
add_action('wp_ajax_nopriv_sign_in', 'ajax_sign_in'); | |
function ajax_sign_in() { | |
$creds = array(); | |
$creds['user_login'] = $_POST['email']; | |
$creds['user_password'] = $_POST['pass']; | |
$creds['remember'] = true; | |
$user = wp_signon( $creds, false ); | |
if ( is_wp_error($user) ) { | |
echo "error"; | |
} else { | |
echo "success"; | |
} | |
die(); // this is required to return a proper result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment