So I have a very basic form which allows users to edit their profile data:
<?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>
Now, how do I implement wp_update_user
in this case to update the user meta only if value is different from the current value or if it is not empty (for example, if they delete the current value in the input field, then it does not get rid of the meta).
Any help will be much appreciated.
the simple answer is You should implement a check like so :
if ( get_user_meta($user_id, 'some_meta_key', true ) != $new_value ){
// old data is different from new data
}
Now, it has not been thoroughly tested by me , but I believe that you need to wrap it in a function and filter the update_user_metadata
action like this :
First add the filter .
add_filter( 'update_user_metadata', 'pre_update_check_meta', 10, 5 );
Then the function
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
}
you just need to use the filters as described, and also add the update_user_meta()
action to your action ( I believe you called it svf_send_message()
) ..