如何在Wordpress中将新的用户元密钥插入数据库

时间:2018-12-06 18:42:00

标签: php jquery html ajax wordpress

我想为wordpress创建自定义注册,并且需要为每个配置文件创建一个新的自定义字段。例如电话领域。除电话,其他信息和个人资料图片字段外,所有其他字段均正常工作,我不知道问题出在哪里。 所以这是我的代码 在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>

以及在我的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;
   }

1 个答案:

答案 0 :(得分:0)

在插入/更新时,如果要插入或更新而不考虑先前的值,请不要使用第4个参数。参见https://codex.wordpress.org/Function_Reference/update_user_meta#Parameters

通过指定第四个参数,您是说仅更新元值= $ prev_value的现有元记录。

使用

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

如果您愿意用该键覆盖任何现有的元记录,或者

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

有时对于检查返回值也很有用。 EG:

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

如果元记录已经存在,将返回false。

并注意update_user_meta的各种返回值:

  • 元ID(如果键不存在);
  • 成功更新时为true;
  • 失败或$ meta_value与现有元值相同时为false 在数据库中。

我认为它现在返回FALSE,因为不存在具有userid,'usermob',false的记录,并且如果您使用

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

它将返回插入(新记录)或TRUE(如果是更新)的meta_id