I have an options page for my plugin. I want the user to be able to click a button to execute a member function of my plugin's class. I found this page:

http://stackoverflow.com/questions/8597846/wordpress-plugin-call-function-on-button-click-in-admin-panel

But I'm not sure if I am interpreting the instructions correctly. I found that creating a form with an action="options-general.php?page=MY_OPTIONS_PAGE_SLUG" will work. Like this:

            <form method="post" action="options-general.php?page=my_options_page_slug">
                <input type="hidden" name="banana" value="yellow">
                <input type="submit" class="button-primary" value="<?php _e('Go') ?>" />
            </form>

Is this what the stackoverflow article is recommending? Is this a decent way to do this? I am not ready to step into (the WP) AJAX just yet. I'm ok with a form and a page refresh.

Thanks for reading.

link|improve this question
1  
Its not the safest way to do it but the processing code is missing so i can't really say, post the rest of you code. – Bainternet Feb 15 at 21:24
On the other side, I have something simple right now like if( $_POST['banana'] == 'yellow' ){ $this->do_something(); }. Will adding a nonce make this method reliable? I am still not sure if the link I provided in the OP meant something like this with the language, "POST that form to your plugin." – rg89 Feb 16 at 2:33
feedback

2 Answers

Try using 'init' action hook to handle the submit trigger. Create a new object of your class in the hooked function and call the member function.

link|improve this answer
Hooking into init would check the $_POST for my variable on every single page load, even for logged out users. Why would I want that? – rg89 Feb 16 at 19:24
Add a conditional check so that the function runs only on your plugins options page only. is_admin should help. – Rutwick Gangurde Feb 17 at 3:54
feedback

Yes, that method should be fine. Adding a nonce is always a positive thing to do as well.

From the WP Codex page: Nonces are used as a security related protection to prevent attacks and mistakes. (Sounds good to me!)

You could also use $pagenow and $typenow variables in place of hardcoding a URL into the form's action attribute.

There are better ways (like AJAX) but I would think this is the "traditional" way to do it.

link|improve this answer
I added a nonce last night. Thanks for the tips on these variables. I'll look them up. – rg89 Feb 16 at 15:13
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.