Reputation: 77
I am trying to see if a user's field value was changed, if it was changed i need to run some code. So, the best way i found to do this was use this filter hook.
https://codex.wordpress.org/Plugin_API/Filter_Reference/update_(meta_type)_metadata
However, for some reason i am getting null if i try to output my $prev_value and my field was not blank before. Why is that?
function myplugin_init() {
add_filter( 'update_user_metadata', 'myplugin_update_foo', 10, 5 );
}
function myplugin_update_foo( $null, $object_id, $meta_key, $meta_value, $prev_value ) {
if ( 'last_name' == $meta_key ) {
echo "<h1>Prev: ". $prev_value ."</h1>";
echo "<h1>New: ". $meta_value ."</h1>";
}
return null; // this means: go on with the normal execution in meta.php
}
add_action( 'init', 'myplugin_init' );
Upvotes: 0
Views: 531
Reputation: 11
I ran into a similar problem. If you call update_user_meta
, you need to add a fourth argument with the previous value. e.g.:
update_user_meta( $user_id, 'custom_field', 'new_value', get_user_meta( $user_id, 'custom_field', true ) );
Only then you will have access to the prev_value
in the update_user_metadata
filter & action.
Upvotes: 1
Reputation: 792
It seems the WordPress action update_user_metadata
only has four parameters.
You are using $prev_value
as a fifth parameter so it will always be null
.
<?php
add_action( 'updated_user_meta', 'my_update_user_meta', 10, 4 );
function my_update_user_meta($meta_id, $object_id, $meta_key, $_meta_value) {
//do stuff
} ?>
Reference: https://codex.wordpress.org/Plugin_API/Action_Reference/updated_(meta_type)_meta
Upvotes: 0