Php 更新用户帖子元数据
因此,我有一个非常基本的表单,允许用户编辑他们的配置文件数据:Php 更新用户帖子元数据,php,wordpress,meta,Php,Wordpress,Meta,因此,我有一个非常基本的表单,允许用户编辑他们的配置文件数据: <?php $current_user = wp_get_current_user(); ?> <div class="rh_contact_form"> <?php echo '<input type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number" value="' .$current_us
<?php
$current_user = wp_get_current_user();
?>
<div class="rh_contact_form">
<?php echo '<input type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number" value="' .$current_user -> billing_phone. '"/>' ;?>
<?php echo '<input type="text" id="rhc_question" name="rhc_question" placeholder="My question" value="' .$current_user -> my_question. '"/>' ;?>
<div class="rh_button">
<button type="button">
<i class="icons">done</i>
</button>
</div>
</div>
完成
现在,我如何实现wp\u update\u user
,在这种情况下,仅当值与当前值不同或不为空时才更新用户元(例如,如果他们删除输入字段中的当前值,则不会删除元)
任何帮助都将不胜感激。简单的答案是您应该执行如下检查:
if ( get_user_meta($user_id, 'some_meta_key', true ) != $new_value ){
// old data is different from new data
}
现在,我还没有对它进行彻底的测试,但我相信您需要将它封装在一个函数中,并过滤update\u user\u metadata
操作,如下所示:
首先添加过滤器
add_filter( 'update_user_metadata', 'pre_update_check_meta', 10, 5 );
然后是函数
function pre_update_check_meta( $null, $object_id, $meta_key, $meta_value, $prev_value ) {
if ( 'some_meta_key' == $meta_key && empty( $meta_value ) ) {
// Now check for NULL or EMPTY or compare with $old_value or whatever you want
return true; // this means: stop saving the value into the database
}
return null; // this means: go on with the normal execution in meta.php
}
您只需使用所述的过滤器,并将
update\u user\u meta()
操作添加到您的操作中(我相信您称之为svf\u send\u message()
)…谢谢您的回复。但是我不太熟悉如何使用函数本身。你能告诉我如何在我关于这个问题的代码中实现它吗?在那之后我应该很好。谢谢。OP中的代码只是一个简单的表单,您没有指定它在哪里(一个插件,在前端,后端?)以及如何提交(ajax?正常操作?)。我编写的代码是实现您要求的检查的示例。如果您不知道如何构造表单提交操作,可以询问新问题或编辑原始问题。您好。谢谢你的回复。我添加了通过ajax提交表单的代码。=)AJAX目前正在工作吗?哪里是cvf\u发送消息功能??您好。这个表格有效。我在问题中添加了函数。我删除了所有敏感信息。但是你会了解整个表单是如何工作的。因此,我试图弄清楚在发送表单时是否可以更新两个字段(电话和“询问”部分)