0

i am having trouble getting any usable information or example of the update_option action hook. my aim is to compare both old and new value of my data, then send email to user for changes. below is the hook:

add_action('update_option', function( $option_name, $old_value, $value ) {
 //....
}, 10, 3)

i have compied the code as is to test run but nothing happened. my question is what goes into $option_name, $old_value, $value? sorry am fairly new to php. any explanation with any example will be appreciated.

EDIT:

below is the full code. the idea is to compare the data if it has changed, then echo:

 add_action('updated_option', function( $options, $old_value, $value ) {
 $options = $options;
foreach ($options as $value) {
    $value['id'] = $old_value;
    $value['id'] = $value;
}
if($old_value == $value){
    echo 'data is current';
else{
    echo 'there has been changes';
  }
}, 10, 3);

i still don't get anything.

5
  • Well, if that //.... is just like that in your actual code, then nothing would happen. And the parameters are: $option_name - the option name; $old_value - the old option value; $value - the new option value. See here.
    – Sally CJ
    Commented Aug 3, 2019 at 0:20
  • @SallyCJ. appreciate the response. i have posted the full code. any idea on what i may be doing wrong? Commented Aug 3, 2019 at 11:18
  • Sorry about the previous comments... that I've deleted. And the main problem in your code is that the $options is the option name and not the option value. If you just want to see if the option has been changed, then add_action( 'update_option_{your_option_name}', function(){ echo 'Option changed'; } ); would do it because if there were no changes, WordPress won't trigger the update_option_your_option_name or updated_option hook.
    – Sally CJ
    Commented Aug 4, 2019 at 4:06
  • @SallyCJ thanks for reply. my option name contains userID. and although your explanation is straight foward, i only want to check for one option change. my code: add_action( 'update_option_{'.$userID.'_interview_date}', function(){ echo 'Option changed'; } );. i did that and nothing happened. if there's another way to achieve this i will be grateful. Commented Aug 4, 2019 at 19:04
  • You shouldn't include the brackets ({ and }).. but anyway, see my answer.
    – Sally CJ
    Commented Aug 5, 2019 at 8:39

1 Answer 1

0

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..';
        }
    } );
    
1
  • 1
    spot on. Checkmarked for perfect example. Commented Aug 5, 2019 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.