Skip to content

Instantly share code, notes, and snippets.

@menslow
Created April 12, 2012 21:12
Show Gist options
  • Save menslow/2371021 to your computer and use it in GitHub Desktop.
Save menslow/2371021 to your computer and use it in GitHub Desktop.
WordPress: Simple sign in function for custom front-end sign in forms.
/**
* mm_sign_in function.
* Sign the user in
* @access public
* @return User errors or true if successful signon
*/
function mm_sign_in() {
if(!is_user_logged_in()) {
if(!empty($_POST)) {
// verify nonce
$nonce = $_POST['signin_nonce'];
if ( ! wp_verify_nonce( $nonce, 'signin_nonce' ) )
{
die();
}
$credentials = array();
$credentials['user_login'] = stripslashes($_POST['signin_username']);
$credentials['user_password'] = stripslashes($_POST['signin_password']);
if ( isset($_POST['signin_remember']) ) {
$credentials['remember'] = (bool)stripslashes($_POST['signin_remember']);
}
// sign the user in
$user = wp_signon($credentials, false);
// prepare response
if ( is_wp_error($user) ) {
return $user->errors;
} else {
wp_redirect( home_url() );
exit();
}
}
return false;
} else {
// user is logged in already
wp_redirect( home_url() );
exit();
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment