2

I am working on a plugin and I want to fire a different action based on each option value change. so how can I do that ? example:

$options = get_option('ACP_settings'); if $options['acp-select'] has changed from previous value than I want to fire different action based on selection

please note that $option['acp-select'] is retrieved from select/option html form and has following values for selection:'book','air','SW','HW' etc....

I hope I have posted my question clearly.

thanks for help

3
  • When do you want to fire that action? When option is being saved? Some time after that?
    – Rarst
    Commented Apr 12, 2014 at 21:58
  • after option has been saved and changed. ie if user selects previous value and saves it then I do not want to fire that action Commented Apr 13, 2014 at 6:32
  • Still looking for solution, any help is appreciated. Commented Apr 13, 2014 at 18:57

1 Answer 1

5

I just came across your question while trying to do a similar thing. I figured out what I needed so I thought I would post an answer here in case someone else finds it useful.

In short I used this filter: https://codex.wordpress.org/Plugin_API/Filter_Reference/pre_update_option_(option_name)

And it looks something like this:

function set_admin_notice_options( $new_value, $old_value ) {

    // Check test mode
    if ( $new_value !== $old_value && ! empty( $new_value ) ) {
        update_option( 'some_option_changed', $new_value );
    }

    return $new_value;
}
add_filter( 'pre_update_option_my_option_name', 'set_admin_notice_options', 10, 2 );

With this method you will then have an option saved if a change is found. Then you can check that option anywhere else and perform any action with it and remove the option afterwards or whatever you wish to do.

Hope you find this helpful.

1

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.