Mathias Schnell
Mathias Schnell

Reputation: 902

How do I perform an action after a user saves an options menu in Wordpress?

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');

Upvotes: 4

Views: 6347

Answers (4)

AuRise
AuRise

Reputation: 2452

This is a bit hacky because there is no action or filter for this specific purpose (surprisingly after 10 years!), but the options.php functionality ends with setting the settings_errors transient, and so you can hook into an action there. But you'll also need additionally validation to ensure your code is being fired on the correct page/permissions, etc.

So here's the hacky action to add:

add_action('set_transient_settings_error', 'maybe_post_save_custom_options');

And the callback with some validation

function maybe_post_save_custom_options {

    $action = array_key_exists('action', $_REQUEST) ? trim(sanitize_key($_REQUEST['action']));
    $form_name = array_key_exists('option_page', $_REQUEST) ? trim(sanitize_key($_REQUEST['option_page']));

    if(!current_user_can('manage_options') || $action !== 'update' || $form_name !== 'options') {
        return; // Bail, user can't update options or not our options page
    }

    // Do stuff
    // data can be accessed via $_POST
    // but also get_option since already saved
    // example...

    $my_option = get_option('my_option_1');

}

If your setting is registered to a section using add_settings_section(), then the value of $_REQUEST['option_page'] will be the slug-name used to identify the section.

I'm not super sure how I feel about it since it's hacky but it does run only once during the save process regardless if any options were actually updated whereas the update_option hook only runs if your option was sent in the $_POST form, missing out on unchecked checkboxes.

Upvotes: 0

Deepak Rajpal
Deepak Rajpal

Reputation: 1011

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/

Upvotes: 1

Marcello Perri
Marcello Perri

Reputation: 620

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

Upvotes: 3

Domain
Domain

Reputation: 11808

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

Upvotes: -5

Related Questions