Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating a registration widget for my wp site and it works as expected, however if the user already exists I need a way of retrieving that information. I'm using ajax to make the call here is my code:

$.post(AjaxRequest.ajaxurl, {
                    action: "send_wlm",
                    user_login: formdata[0].value,
                    user_email: formdata[1].value,
                    user_practice_role: formdata[2].value,
                    user_practice_level: formdata[3].value,
                    user_practice: formdata[4].value
                }, function (response) {
                    console.log(response);
                });

PHP

public function wlm_user_level (){
    global $wpdb;
    $random_password = wp_generate_password( $length=12,     $include_standard_special_chars=false );
    $user_name = $_POST['user_login'];
    $user_email = $_POST['user_email'];
    $user_id = wp_create_user( $user_name, $random_password, $user_email ); }

My only question is, I know that it returns an error object but how do I get that object to return it from my ajax call?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

In your example code, $user_id will be the error object. You can test if is_wp_error and return the error string via get_error_message:

$user_id = wp_create_user( $user_name, $random_password, $user_email );
if ( is_wp_error( $user_id ) )
   return $user_id->get_error_message();
share|improve this answer
    
DOH! I actually have that implementation later on in my code: if (!is_object($user_id)) {...} it's been a long day, thanks! –  richbai90 Jul 10 '13 at 21:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.