【问题标题】:how to insert a new user meta key into database in wordpress如何在wordpress中将新的用户元键插入数据库
【发布时间】:2019-05-08 12:37:05
【问题描述】:

我想为 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;
   }

【问题讨论】:

  • $childsdetails 里面真的有东西吗?由于codex.wordpress.org/Function_Reference/update_user_meta 表示如果值为空,则元不会被删除,如果值中没有任何内容,则可能不会被插入。
  • 是的,但在我的整个代码中不在这里,它工作正常...问题是移动、个人资料图像和额外信息字段未保存在数据库中
  • 啊,这是唯一一个你没有指定 prev 值的地方。

标签: php jquery html ajax wordpress


【解决方案1】:

在插入/更新时,如果要插入或更新,请不要使用第 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

有时检查返回值也很有用。例如:

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

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

并注意 update_user_meta 的各种返回值:

  • 如果密钥不存在,则元 ID;
  • 更新成功时为真;
  • 如果失败或 $meta_value 与现有元值相同则为 false 在数据库中。

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

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

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

【讨论】:

    猜你喜欢
    • 2021-02-18
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 2014-03-24
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    相关资源
    最近更新 更多