Home > Blockchain >  Wordpress get previous value for add_user_contact_method in hooks
Wordpress get previous value for add_user_contact_method in hooks

Time:01-09

I implemented a Secondary mail field using the add_user_contact_method function

 function add_user_contact_method(array $methods)
{
    // Add user contact methods
    $methods['secondary_mail']   = __('Secondary Mail');

    return $methods;
}
add_filter('user_contactmethods', 'add_user_contact_method');

My problem is that I try to do something when this mail is changed and I need the old value.

add_action('profile_update', 'DBGrename_secondary_mail', 9, 3);

function DBGrename_secondary_mail(int $user_id, WP_User $old_user_data, array $userdata){ 
        
    debug('Entered : ' . __FUNCTION__);
    debug(' Id : ' . $user_id);
    debug(' Old Secondary Mail : ' . $old_user_data->secondary_mail);
}

But $old_user_data->secondary_mail return the new value, and not the old one, so I tried with update_user_metadata, but there the previous value is empty:

add_action('update_user_metadata', 'DGB_User_Meta', 1, 5);

function DGB_User_Meta(null|bool $check, int $object_id, string $meta_key, mixed $meta_value, mixed $prev_value) {
    debug('Entered : ' . __FUNCTION__);
    debug(' ObjectId : ' . $object_id);
    debug(' MetaKey : ' . $meta_key);
    debug(' MetaValue : ' . $meta_value);
    debug(' Previous Value : ' . print_r($prev_value, true));

}

Does someone have an idea, of how I could get the previous value when someone changes this field in the user admin page?

CodePudding user response:

I found a workaround, using update_user_meta, and calling get_metadata from inside there, I get what I want, but I still believe the other functions should work.

add_action('update_user_meta', 'DGB_User_Meta', 1, 4);

function DGB_User_Meta(int $meta_id, int $object_id, string $meta_key, mixed $_meta_value) {
    debug('Entered : ' . __FUNCTION__);
    debug(' MetaId : ' . $meta_id);
    debug(' ObjectId : ' . $object_id);
    debug(' MetaKey : ' . $meta_key);
    debug(' MetaValue : ' . print_r($_meta_value, true));
    debug(' GetMeta : ' . get_metadata('user', $object_id, $meta_key, true));

}
  • Related