1

I have written a code for inserting wordpress user if I try to add new user I am getting 3 warning errors saying like below:

"Warning: mysql_real_escape_string() expects parameter 1 to be string, object given in /home/intecom/public_html/app/wp-includes/wp-db.php on line 1129 " ,

"Warning: Illegal offset type in isset or empty in /home/intecom/public_html/app/wp-includes/cache.php on line 725"

and

"Warning: array_key_exists(): The first argument should be either a string or an integer in /home/intecom/public_html/app/wp-includes/cache.php on line 725".

my code is below:

  $first_name = 'my_firstname_value';
  $last_name = 'my_lastname_value';
  $trainer_email = 'trainer@example.com';
  $course_registered = Array
                      (
                        [0] => 2410
                      );
  $pro_icon_status = '1';
  $this->institute_icon[0] = '1';
  $this->institute_name[0] = 'my_institute_name';
  $user_data = array(
                    'user_login' => $first_name,
                    'user_pass' => '',
                    'user_email' => $trainer_email,
                    'first_name' => $first_name,
                    'last_name' => $last_name,
                    'role' => 'trainer'
                );
                $random_password = wp_generate_password(8,false);
                $user_id = wp_insert_user( $user_data );

                update_user_meta( $user_id, 'course_registered',$course_registered );
                update_user_meta( $user_id, 'inistitute_name',$this->institute_name[0] );
                update_user_meta( $user_id, 'inistitute_icon',$this->institute_icon[0] );
                update_user_meta( $user_id, 'trainer_icon',$pro_icon_status );
                wp_set_password($random_password, $user_id);

My input is only either string (or) array but why it is saying that "Iam passing object" I don't know what is happening. It is driving me nuts. can any one please tell me what was my mistake? Thanks in advance.

Update: The problem is not with 'wp_insert_user' or 'update_user_meta', actual problem is with the data that I was giving i.e. Am giving the same user_login name which is already there in the database, I am getting the WP_Error Object as below:

  WP_Error Object
  (
    [errors] => Array
    (
        [existing_user_login] => Array
            (
                [0] => Sorry, that username already exists!
            )

    )

    [error_data] => Array
                  (
                  )

    ) 

Now can u plz tell me how to show that error to user on the front end?

0

The problem is from wp_insert_user and the fact that your value for user_pass is empty.

You should update your code as so

 $random_password = wp_generate_password(8,false);
 $user_data = array(
                'user_login' => $first_name,
                'user_pass' => $random_password,
                'user_email' => $trainer_email,
                'first_name' => $first_name,
                'last_name' => $last_name,
                'role' => 'trainer'
            );

And remove this line wp_set_password($random_password, $user_id);

You should also check the value of your $user_id to make sure it's not returning a WP_Error object You can use var_dump($user_id')

  • The problem is not with the user_pass field is empty, The actual problem with the WP_Error object as u said secondly. As I am trying to give the same username for 'firstname' field that is I am getting the error. Now can u plz tell me how to handle that error to show to the user if any error? Am updating Question with error. – Prasad Patel Dec 18 '16 at 20:46
  • @ Tunji Ayoola, Thanks, much appreciated ur efforts. as u said '$user_id' is returning 'WP_Error object' thats why I keep getting that error. since I am passing the same 'user_login' name I used before I got that error. I fixed it by replacing 'user_login' => $first_name, with 'user_login' => $trainer_email. I used 'trainer_email ' instead of '$first_name'. – Prasad Patel Dec 19 '16 at 11:36
0

you cannot use mysql_real_escape_string() without a link established to mysql. You have used the mysql_real_escape_string() function before connecting to the mysql.

  • first, Thanks for your quick reply. You said "I have used the mysql_real_escape_string() function before connecting to the mysql." how I dont know could u plz tell me how and what should I fix that? – Prasad Patel Dec 16 '16 at 12:46
0

Hello you have to do some debugging on your code by making sure that all the variables passed to update_user_meta() are strings or arrays and not objects.

FROM THE CODEX

The new desired value of the meta_key, which must be different from the existing value. Arrays and objects will be automatically serialized. Note that using objects may cause this bug to popup.

  • I am passing array only but I don't know why I am getting this error! I am writing the data which am passing to update_user_meta in my question above, plz check it once. – Prasad Patel Dec 17 '16 at 13:20
  • I am giving only string & array but I am not giving any object but why it was saying "I am passing object". This is a basic thing but I don't know why it is not working. Please can any one help me.. – Prasad Patel Dec 18 '16 at 17:42
  • @prasadchinthala Do you have WP_DEBUG turned on? – Tunji Dec 18 '16 at 20:06
  • yes I tried that too but getting the same error, no better debugging. – Prasad Patel Dec 18 '16 at 20:08
  • just now came to know that actual problem is with 'wp_insert_user', for that I have posted a new question, plz check this wordpress.stackexchange.com/questions/249656/… – Prasad Patel Dec 18 '16 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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