WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a plugin with a settings/option page and I want some php code to be executed whenever someone saves the settings on that page and that page alone. Which actions do I need to hook into to accomplish this?

share|improve this question
up vote 2 down vote accepted

If you're using Settings API, then you should use a sanitize callback:

register_setting( $option_group, $option_name, $sanitize_callback );

The myth is that the $sanitize_callback actually is a filter for your options when it's saved in the database. This is the place where you can do something with your custom code.

This is the sample code:

register_setting( 'wpse38180', 'wpse38180', 'sanitize_wpse38180' );

function sanitize_wpse38180( $options )
    // Do anything you want

    return $options;
share|improve this answer

There is a pre-update filter, applied just after the sanitize filter. It's called pre_update_option_optionname and takes the new and old values of the option as parameters. Use it like this:

register_setting( $my_option_group , $my_option_name );
// 10 is the filter priority, 2 is the number of arguments
add_filter( 'pre_update_option_' . $my_option_name , 'my_callback', 10, 2 );

function my_callback( $newvalue, $oldvalue ) {
    // $newvalue has already been sanitized, but not yet saved
    // do whatever you like here
    return 'filtered value';

The benefit of using this is that the intent is clearer, and you don't have to manually get the old option value (if you need it) since it's passed in for you.

This filter is specific to a field, so if you want it to run once when saving options, just add it to only one field.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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