2

I have a Wordpress plugin in which I added a menu that allows a user to set various options. I used the "add_menu_page" function described here - http://codex.wordpress.org/Function_Reference/add_menu_page . I now need to do some actions AFTER the options on this page have been saved, but I'm not sure if I can do that or, if I can, what hooks to use.

I can't post exactly the code I'm using to create this options page, but I'll try and put together something similar to get the message across

function my_menu_page() {
    add_menu_page('My Menu', 'My Menu', 'edit_posts', "my_menu_settings", "my_menu_settings");
}

function my_menu_settings() {
    echo "          
        <h1>My Menu Settings</h1>
        <div>
            <form action='options.php' method='post' name='options'>
            " . wp_nonce_field('update-options') . "
                <input type='text' name='my_option_1' value='" . get_option('my_option_1') . "' /><br />
                <input type='hidden' name='action' value='update' />
                <input type='hidden' name='page_options' value='my_option_1' />     
                <input type='submit' name='Submit' value='Update' />
            </form>
        </div>
    ";
}

add_action('admin_menu', 'my_menu_settings');
Mathias Schnell
  • 884
  • 3
  • 13
  • 22

3 Answers3

2

I think you are looking for something like update_option, here is the link to the Wordpress documentation

Here are a few variations of the same hook: (update_option can be used also to save/create new options)

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_menu_settings', 10, 2);

update_option: Runs before an option gets updated. Example:

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

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

add_action('updated_option', 'my_menu_settings', 10, 3);

Thanks

Marcello Perri
  • 572
  • 7
  • 22
  • Do these fire when the actual sidebar page is updated or the ACF field group is updated? – Taylor A. Leach Oct 06 '20 at 21:39
  • According to the documentation "Fires immediately before an option value is updated", so when any option value is updated including the value mentioned by you. you can always try it – Marcello Perri Oct 07 '20 at 08:58
  • so not when the options page itself is updated. I have tried but it doesn't seem to fire. Maybe I am naming it wrong, not 100% what the name foo is referring to. – Taylor A. Leach Oct 07 '20 at 13:23
  • 'foo' is a generic option name, lets say that you create an option name called "headerSize", in this case `add_action('update_option_headerSize', 'my_menu_settings', 10, 2); ` the function my_menu_settings will be fired when headerSize will be updated. you can always create a function that kills the flow and try to see when the function is fired. – Marcello Perri Oct 07 '20 at 13:31
1

In case someone wants to do some action/sanatization before saving, he can use pre_update_option_foo hook

add_filter( 'pre_update_option_foo', 'myplugin_update_field_foo', 10, 2 );

See detail/example in Codex: https://developer.wordpress.org/reference/functions/update_option/

Deepak Rajpal
  • 811
  • 1
  • 8
  • 11
-5

You can use WordPress action hook 'save_post' to add actions after the page has been saved. More information and examples of this WordPress hook read http://codex.wordpress.org/Plugin_API/Action_Reference/save_post

Domain
  • 11,562
  • 3
  • 23
  • 44
  • Is save_post hook suitable for this need? I guess the original question is more related to save settings page (options.php) and not saving a normal post or custom post type in admin. Instead, I find this response more suitable for the original needs: http://wordpress.stackexchange.com/questions/79898/trigger-custom-action-when-setting-button-pressed using admin_post hook. – julianm Jan 20 '15 at 14:52