-
-
Save reinislejnieks/7311600 to your computer and use it in GitHub Desktop.
wp register new user #wp
This file contains hidden or 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
/** | |
* mm_register function. | |
* Register a new user. | |
* @access public | |
* @return User errors or user is logged in. | |
*/ | |
function mm_register() { | |
if(!is_user_logged_in()) { | |
if(!empty($_POST)) { | |
// verify nonce | |
$nonce = $_POST['register_nonce']; | |
if ( ! wp_verify_nonce( $nonce, 'register_nonce' ) ) | |
{ | |
die(); | |
} | |
global $notice_key; // global notice key for setting notice messages | |
$userdata = array(); | |
$userdata['user_email'] = stripslashes($_POST['register_email']); | |
$userdata['user_login'] = stripslashes($_POST['register_username']); | |
$userdata['user_pass'] = stripslashes($_POST['register_password']); | |
// insert the user | |
$user_id = wp_insert_user( $userdata ); | |
if ( is_wp_error($user_id) ) { | |
// set error msg notice | |
if(isset($user_id->errors['empty_user_login'])) { | |
$notice_key = 'empty_user_login'; | |
} elseif(isset($user_id->errors['existing_user_login'])) { | |
$notice_key = 'existing_user_login'; | |
} else { | |
$notice_key = 'insert_user_error'; | |
} | |
return $user_id->errors; | |
} else { | |
// log user in | |
$credentials = array(); | |
$credentials['user_login'] = stripslashes($_POST['register_username']); | |
$credentials['user_password'] = stripslashes($_POST['register_password']); | |
$credentials['remember'] = false; | |
$user = wp_signon( $credentials, false ); | |
// prepare response | |
if ( is_wp_error($user) ) { | |
// set error msg notice | |
if(isset($user->errors['invalid_username'])) { | |
$notice_key = 'invalid_username'; | |
} elseif(isset($user->errors['incorrect_password'])) { | |
$notice_key = 'incorrect_password'; | |
} else { | |
$notice_key = 'signon_error'; | |
} | |
return $user->errors; | |
} else { | |
wp_redirect( home_url() ); | |
exit(); | |
} | |
return false; | |
} | |
} | |
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