Is this the right way to execute some PHP in my plugin after a button click on an options page?

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

Read More

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.

Related posts

Leave a Reply

2 comments

  1. 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.