1

I am creating a Wordpress theme from scratch and I would like to save a file with the theme option anytime the theme options change.

I was thinking about:

do_action('admin_init', 'my_custom_function');
function my_custom_function(){
    save_file();
    ...
}

but in this case the file will be save anytime the admin area is loaded.

On the dashboard the theme creates forms to manage the theme options, like the following:

<form method="post" action="options.php">

    <?php settings_fields( 'theme-settings-group' ); ?>
    <?php do_settings_sections( 'theme-settings-group' ); ?>

    // form in here
    <?php submit_button(); ?>

</form>

This form send information to options.php which save the theme options on the database based on the setting group specified.

I would like to trigger an function any time something is saved in "options.php".

Can you guys suggest please? Thannks

1
  • Welcome to WordPress Development. WordPress has a special API for themes to store options which I do not currently understand well enough to explain but others do. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. Jun 10, 2020 at 9:14

1 Answer 1

0

Here is the solution:

update_option_{option_name}: Runs after the option with name "option_name" has been updated. For example, for the option with name "foo":

 add_action('update_option_foo', 'my_custom_function', 10, 2);

update_option: Runs before an option gets updated. Example:

add_action('update_option', 'my_custom_function', 10, 3);

updated_option: Runs after an an option has been updated. Example:

add_action('updated_option', 'my_custom_function', 10, 3);
1
  • It should be mentioned that the hooks updated_option and update_option_{$option} which run after tan option has been saved, will not fire if the value of the option is the same as the previously saved ones. Some suggest to use pre_update_option_{$option} however this hook is fires just before the values have been updated into the database which might not work depending on your callback needs.
    – Aedan
    Feb 23, 2023 at 18:47

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.