Looking at the full code in the updated question, it seems that you’re just checking if the option has been changed; so for that purpose, you could simply do so, where <option name>
is the option name like my_option
:
add_action( 'update_option_<option name>', function(){
// you shouldn't echo, but this is just for testing
echo 'option changed';
} );
And if the option’s value is actually the same as the old one, the above function won’t be called. Because that’s how update_option()
works — if it’s called but no changes in the option’s value, then the hook update_option_<option name>
won’t be called. And neither does the updated_option
or update_option
hook.
Now you said, “my option name contains user ID” and mentioned that the option name is <user ID>_interview_date
. And in that case, you can do something like this:
-
Option 1: Validates the user ID/data:
add_action( 'updated_option', function( $option_name ){ if ( preg_match( '/^(\d+)_interview_date$/', $option_name, $matches ) ) { $user_id = absint( $matches[1] ); // Check if the user exists. if ( $user_id && ( $user = get_userdata( $user_id ) ) ) { // you shouldn't echo, but this is just for testing echo $option_name . ' option changed'; } } } );
-
Option 2: No validations..
add_action( 'updated_option', function( $option_name ){ if ( preg_match( '/^(\d+)_interview_date$/', $option_name, $matches ) ) { // you shouldn't echo, but this is just for testing echo $option_name . ' option changed; no validations performed..'; } } );