Support » Plugins » Hacks » hook function for plugin options saved

  • I was trying and googling for a while, but maybe I did not use the right keywords.

    I am writing a plugin. I have a settings for that saves just fine.

    I’d like to add a hook, to be able to either do some php stuff either before or after all my options are saved.

    I thought I could find some code like

    add_action(“options_saved”, “my_function”);

    I found several posts, but they seem do not seem to cover my concern.

    Moreover: Would it be possible to pass data to such a hooked function, that will not be saved? I want to test the configuration on the server side and like to pass some user entered test data, that must not be saved.

    THanks a lot and kind regards

Viewing 7 replies - 1 through 7 (of 7 total)
  • If you’re adding your own hook, you need to use do_action() instead. That’s what runs any action hooks that are registered for your hook name.

    As a (not tested) example…

    $values = do_action ('before_my_action_hook', $values);
    $values = update_option('my_settings', $values);
    $values = do_action ('after_my_action_hook', $values);

    Adding a variable to each of those functions is reduntant, not to mention that it would overwrite the contents on $values. Also the update_option in this case would also overwrite the contents of $values. Try this:

    do_action( 'before_options_saved', $values );
    $updated = update_option( 'my_options', $values );
    do_action( 'after_options_saved', $values, $updated );

    This way you can check if the options was updated as update_option() returns a boolean.

    Well, thanks a lot.

    I understand that do_action can be used to trigger my own defined hook?

    BUT: WHERE should I call it?

    my module looks like this:

    add_action('admin_init', 'strong_auth_init' );
    add_action('admin_menu', 'strong_auth_add_menu');
    add_action('update_option_strong_authentication_server', 'strong_auth_on_save_changes');
    add_action('update_option_strong_authentication_verify_host', 'strong_auth_on_save_changes');
    add_action('update_option_strong_authentication_verify_peer', 'strong_auth_on_save_changes');
    add_action('update_option_strong_authentication_realm', 'strong_auth_on_save_changes');

    I never call “update_option” anywhere.

    In addition: In my functon strong_auth_on_save_changes retrieves the
    settings like this:


    But I’d like to have fields in my form, that are NOT saved as settings, since I’d like to use those fields as test data, if the plugin is configured right.

    I am afraid I can not make myself quite clear.

    THanks a lot

    WHERE should I call it?

    Where does it need to be called? What you’ve posted doesn’t give any insight into the code behind your plugin, but it’s really not that important.

    The simple answer is you do your call to do_action() in your code where it’s needed. It’s completely different from add_action(), so don’t think that they need to go together at all, that will only get you into trouble.

    As a very short example…

    add_action('init', 'my_function');
    function my_function () {
        $my_var = array (1, 2, 3, 4);
        $my_var = do_action ('change_my_var', $my_var);
        var_export ($my_var);

    Then any other plugin can add add_action ('change_my_var', 'plugin_function'); to update the values before you use them. Does that all make sense?

    Thanks a lot for the patience. I think I got it.

    But I think we also got on the wrong foot here.

    What I wanted to achieve is, to verify the plugin settings ideally before they are saved.
    As I need to do a web request to verify these settings, I can not use any javascript logic.

    And now what confuses me: I did not write any code to actually save the settings – thus I have no code location, where I can test them.

    What I have done so fat is:

    1. register the settings in my function strong_auth_init using

    register_setting("strong_auth", "strong_authentication_server");

    and so forth which is hooked to:

    add_action('admin_init', 'strong_auth_init' );

    2. The form for the settings is displayed and the settings are saved.

    function strong_auth_add_menu() {
    	add_options_page("Strong Authentication", "Strong     Authentication", 10, __FILE__,"strong_auth_display_options");

    to the admin_menu.

    Everything works fine so far.
    But all the settings-saving is obviously abstracted from my code. That’s cool, but not nice for server side validation.

    So I created the hook

    add_action('update_option_strong_authentication_server', 'strong_auth_on_save_changes');

    So I have a function function strong_auth_on_save_changes() that is called when the settings are saved, but obiously the settings are already saved.

    And it might be that saving faulty settings might break the wordpress/plugin functionality.

    Kind regards

    OK, that does make things very different.

    To do that you don’t need actions. You need to make the call to whatever validation functions that you’re going to use and check the result from that.

    So how are you going to perform the validation? that’s pretty much what it comes down to, and without you getting all of that owrked out first, there’s no point trying to do anything else because no one (including you) will know what you’re actually working towards.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘hook function for plugin options saved’ is closed to new replies.