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