2015-09-21 53 views
0

所以,我有我的wordpress網站形式如下:更新用戶的元數據

<?php 
    $current_user = wp_get_current_user(); 
?> 

<div class="rh_contact_form">  
    <div class="mdl-textfield rhc_phone_number_class"> 
     <?php if (is_user_logged_in()) { ?> 
      <?php echo '<input type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number" value="' .$current_user -> rh_phone. '"/>' ;?> 
     <?php }else{ ?> 
      <input class="mdl-textfield__input" type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number" value=""/> 
     <?php } ?>            
    </div> 

    <div class="rhc_ask"> 
     <textarea class="mdl-textfield__input" type="text" rows= "3" id="rhc_ask" name="rhc_ask" placeholder="Ask seller"></textarea>   
    </div> 
    <input type="submit">Submit</input> 
</div> 

因此,有兩個用戶metakeys:rh_phonerh_ask

如何更新爲metavalue這兩個?

謝謝!

回答

1

您需要在表單處理函數中添加類似於此的內容。還要在該函數內部調用「當前用戶」,或通過表單中的隱藏字段傳遞該ID。

$current_user = wp_get_current_user(); 
$rh_phone = sanitize_text_field($_POST['rh_phone']); 
$rh_ask= sanitize_text_field($_POST['rh_ask']); 

if(isset($rh_phone)){ 
    update_post_meta($current_user, 'rh_phone', $rh_phone); 
} 
if(isset($rh_ask)){ 
    update_post_meta($current_user, 'rh_ask', $rh_ask); 
} 

你可以在這裏找到更多關於update_post_meta()的信息:https://codex.wordpress.org/Function_Reference/update_post_meta。我希望有所幫助。