最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hooks - Trigger a custom function when option are saved in admin area

programmeradmin0浏览0评论

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

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

Share Improve this question edited Jun 10, 2020 at 9:07 Marcello Perri asked Jun 10, 2020 at 9:01 Marcello PerriMarcello Perri 1117 bronze badges 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. – Matthew Brown aka Lord Matt Commented Jun 10, 2020 at 9:14
Add a comment  | 

1 Answer 1

Reset to default 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);
发布评论

评论列表(0)

  1. 暂无评论