This is bugging the heck out of me. Essentially I've narrowed the issue down to one line of code:
$user_id = wp_insert_user ($userdata);
From what I can tell nothing should be echoed here, but I should get the user_id returned from the wp_insert_user function. What I'm getting though is the email and other values echoed back.
I will paste the code for the entire function at the moment, but I've tested it apart and this is the line of code causing the echo in my ajax call, messing up my return because its not formatted in my JSON variable. See code below (Thanks you in advance for any help I can get with this, really need it at this point)
public function registerUser(){
$event = $_REQUEST['event'];
$nonce=$_REQUEST['ajax_nonce'];
if (! wp_verify_nonce($nonce, 'ajax_nonce') ) {
$response = json_encode(
array(
'success' => false,
'html' => 'Please stop trying break me. For security reasons your IP has been logged'
)
);
header( "Content-Type: application/json" );
echo $response;
die();
}
global $wpdb;
$html = "";
//We shall SQL escape all inputs
$user_login = $wpdb->escape($_REQUEST['username']);
if(empty($user_login)) {
$error = 1;
$html .= "<li>Username could not be created because you have not completed your first and last name.</li>";
}
$user_email = $wpdb->escape($_REQUEST['email']);
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $user_email)) {
$error = 1;
$html .= "<li>Please enter a valid email.</li>";
}
$user_pass = $wpdb->escape($_REQUEST['password']);
$confirm_password = $wpdb->escape($_REQUEST['confirm_password']);
if(empty($user_pass) || $user_pass != $confirm_password){
$error = 1;
$html .= "<li>Passwords do not match.</li>";
}
if(empty($error)){
$first_name = $_REQUEST['first'];
$last_name = $_REQUEST['last'];
$display_name = $_REQUEST['first']." ".$_REQUEST['last'];
$userdata = compact('user_login', 'user_email', 'user_pass', 'first_name', 'last_name', 'display_name');
$user_id = wp_insert_user( $userdata );
if ( is_wp_error($user_id) ){
print_r($status);
$error = 1;
$html = "<b>Username already exists</b>. Please try another one or <a href='".get_bloginfo('url')."/contact'>contact us</a>";
} else {
update_usermeta( $user_id, 'phone', $_POST['phone'] );
$from = get_option('admin_email');
$headers = 'From: '.$from . "\r\n";
$subject = "Painter Registration Successful!";
$msg = "<h2>Registration successful!</h2><h3>Your login details:</h3><br/><b>Username</b>: $username<br/><b>Password</b>: $password<br/><br/>Thank you again for joining Brush Party, now you can login and finish signing up for the <a href='".get_bloginfo('url')."/registration/?event=$event'>next party here!</a>";
$html = "Awesome! You are officially a registered painter at Brush Party. That was easy, and you thought painting was going to be hard... Now you just need to check your email for you login details";
}
}
$response = json_encode(
array(
'success' => $status = (empty($error)) ? true : false,
'first' => $_REQUEST['first'],
'last' => $_REQUEST['last'],
'phone' => $_REQUEST['phone'],
'email' => $_REQUEST['email'],
'html' => $html
)
);
add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
wp_mail( $email, $subject, $msg, $headers );
// response output
header( "Content-Type: application/json" );
echo $response;
die(); // WP AJAX calls must die
}
EDIT:
I've since looked at the user.php file and targeted the issue at line 1371, here's the code there
if ( $update )
do_action('profile_update', $user_id, $old_user_data);
else
do_action('user_register', $user_id);
var_dump
somewhere :) Maybe in a filter or action.add_action( 'wp_ajax_registerUser', array( $this, 'registerUser' ) ); add_action( 'wp_ajax_nopriv_registerUser', array( $this, 'registerUser' ) );