msab
msab

Reputation: 27

how to insert a new user meta key into database in wordpress

I want to create a custom registration for wordpress and I need to create a new custom field for each profile. For example a phone field. all fields are working except the phone, extra info and profile picture fields and i don't know where is the problem. so here is my code in registration.php

<input type="text" class="input" id="firstname" name="firstname" placeholder="First name" required>
<input type="text" class="input" id="lastname" name="lastname" placeholder="Last name" required>
<input type="email" class="input" name="mail" id="mail" placeholder="E-mail" required>
<input type="password" id="password" minlength="8" class="input" name="password" placeholder="Password" required>
<input type="file" class="input" id="profileimg" name="profileimg" title="Profile image" placeholder="Profile image">
<input type="button" class="input buttonSubmitFile" value="Profile image">
<input type="text" class="input" minlength="11" id="tel" name="tel" placeholder="Mobile " required>
<input type="submit" class="btnSubmit btn-Blue" value=Register>
    <script>
          var submit_button      = $(".btnSubmit"); 
     $(submit_button).click(function(e){
              e.preventDefault();
                 var fname = $( "#firstname" ).val();
                  var lname = $( "#lastname" ).val();
                  var mail = $( "#mail" ).val();
                  var tel = $( "#tel" ).val();
                  var pass = $( "#password" ).val();
                 var uid = $( "#userid" ).val();
               var aFormData = new FormData();
     aFormData.append("profileimg", $('#profileimg')[0].files[0]); 
              aFormData.append("action", "trip_register_ajax"); 
              aFormData.append("f_name", fname); 
              aFormData.append("l_name", lname); 
              aFormData.append("e-mail", mail); 
              aFormData.append("mob", tel); 
              aFormData.append("pass", pass);
              aFormData.append("u_id",uid);
      jQuery.ajax({
                    url : '<?php echo admin_url( 'admin-ajax.php'); ?>',
                    type : 'post',
                    processData: false,
                    contentType: false,
                    dataType: "json",
                        data :  aFormData  ,
                        success : function( response ) {
                            $('.message-response').show().html(JSON.stringify(response));
                        },
                        error: function(response) {
                            $('.message-response').show().html(response.responseText);
                        }
                  }); 
             }); 
    </script>

and in my functions.php

    add_action( 'wp_ajax_nopriv_trip_register_ajax', 'trip_register_ajax' );
add_action( 'wp_ajax_trip_register_ajax', 'trip_register_ajax' );
function trip_register_ajax() {
    $firstname=$_POST['f_name'];
    $lastname=$_POST['l_name'];
    $mail=$_POST['e-mail'];
    $tel=$_POST['mob'];
    $extra_information = $_POST['information'];
    $upload_image= $_FILES['profileimg']['name'];
    $filepath = "uploads/".$upload_image;
    $filetmp  = $_FILES["profileimg"]["tmp_name"];
    move_uploaded_file($_FILES['profileimg']['tmp_name'], __DIR__.'/../../uploads/'. $upload_image);
    $nicename=$firstname." ".$lastname;
$userdata = array(
        'user_login'  =>  $nicename,
        'first_name'       => $firstname,
        'last_name'   => $lastname,
        'user_email'    => $mail,
        'user_pass'   =>  $_POST['pass'] 
    );
$exists_email = email_exists( $mail );
$exists_username = username_exists( $mail );
if ( $exists_email || $exists_username) {

    echo "Your Email Already Registered";
}else{
   $user_id = wp_insert_user( $userdata ) ;
    $childsdetails = maybe_unserialize($childsdetails);
    update_user_meta( $user_id, 'user_child_details', $childsdetails );
    update_user_meta( $user_id, 'user_extra_info', $extra_information, false ); 
     update_user_meta( $user_id, 'user_mob', $tel , false );
        echo "You Have Registered Successfuly";
       }
    die;
   }

Upvotes: 0

Views: 4391

Answers (1)

anmari
anmari

Reputation: 4163

When inserting / updating, don't use the 4th parameter if you want to insert OR update regardless of the previous value. See https://codex.wordpress.org/Function_Reference/update_user_meta#Parameters.

By specifiying a fourth parameter you are saying ONLY update existing meta records where the metavalue = $prev_value.

Use

update_user_meta( $user_id, 'user_extra_info', $extra_information ); 
update_user_meta( $user_id, 'user_mob', $tel );

if you happy to overwrite any existing meta record with that key, OR

https://codex.wordpress.org/Function_Reference/add_user_meta

Also sometimes useful to check the return values. EG:

add_user_meta( $user_id, 'user_mob', $tel, true );  

would return false if a meta record already existed.

and note the variety of return values for update_user_meta:

  • Meta ID if the key didn't exist;
  • true on successful update;
  • false on failure or if $meta_value is the same as the existing meta value in the database.

I think it is returning FALSE now because a record with userid, 'usermob', false doesnt exist and if you used

update_user_meta( $user_id, 'user_mob', $tel );

it will return the meta_id for an insert (new record) or TRUE (if it was an update)

Upvotes: 1

Related Questions