9

Weird question, how can I run a custom function AFTER my theme settings are saved? I see that the register_settings() function has a sanitize callback, but that runs before the settings are changed. I need something to run after they're saved. Is there an action or filter I can use to do that?

EDIT -

SO I have this setting I made:

add_settings_section(
    'kjd_body_background_settings_section', // ID hook name
    null, // label
    null, // function name
    'kjd_body_background_settings' // page name
);


    add_settings_field(
        'kjd_body_background_colors', // ID hook name
        null, //label
        null, //callback
        'kjd_body_background_settings', // page name
        'kjd_body_background_settings_section' // parent section 
);

and this function:

function kjd_update_stylesheet( $oldvalue, $_newvalue ){
   echo 'updated!';
   die();
}
add_action('update_options_kjd_body_background_settings','kjd_update_stylesheet',10, 2);

//add_action('update_options_kjd_body_background_colors','kjd_update_stylesheet',10, 2);
17

Use the filter update_option_{$option}. It runs after a successful saving.

$option is the name of your option, and you get the old and the new value as parameters.

From wp-includes/option.php:

do_action( "update_option_{$option}", $oldvalue, $_newvalue );

Use it like this for an option wpse_themesettings:

add_action( 'update_option_wpse_themesettings', 'wpse_check_settings', 10, 2 );

function wpse_check_settings( $old_value, $new_value )
{
    // do something
}
  • Hmmm ok. So if I have dozens of options, over dozens of pages/options groups I would have to make a hidden option for each page to run the same function? – rugbert Aug 12 '13 at 11:42
  • No, you just use the names of the options you want to save. No extra hidden options. – fuxia Aug 12 '13 at 12:04
  • yeah I thought of that on my way into work and said "duh"! Thanks for the answer though, you just made my project 100% more awesome – rugbert Aug 12 '13 at 14:10
  • Hey, just so we are clear. When you say to use update_option{$option} you mean the $page argument in the register_settings() function right? so like add_settings_field( $id, $title, $callback, $page, $section, $args ); Or is it the ID name? – rugbert Aug 12 '13 at 23:49
  • Also - I just tried both and they didnt seem to fire the custom. See my updated question for the code I used – rugbert Aug 12 '13 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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