1

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:

https://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.

2
  • 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
    Commented Feb 15, 2012 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
    Commented Feb 16, 2012 at 2:33

2 Answers 2

1

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.

2
  • 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
    Commented Feb 16, 2012 at 19:24
  • Add a conditional check so that the function runs only on your plugins options page only. is_admin should help. Commented Feb 17, 2012 at 3:54
0

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.

1
  • I added a nonce last night. Thanks for the tips on these variables. I'll look them up.
    – rg89
    Commented Feb 16, 2012 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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