There are some great resources on how best to add a form to your plugin. Notably, there’s this from Tutsplus (They write very solid tutorials; Love them):
http://code.tutsplus.com/tutorials/create-a-custom-wordpress-plugin-from-scratch–net-2668
In it, Cristian proposes that you use this for your form’s action
<form action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" method="post" name="oscimp_form"><input name="oscimp_hidden" type="hidden" value="Y" /></form>
and use this to handle the submission:
if($_POST['oscimp_hidden'] == 'Y') { //Form data sent } else { //Normal page display }
This is fine if you aren’t using OOP in your plugin; if you are (and you really should be IMHO), there are a number of ways to go about it. One is to point the action to a plugin menu item and then in the handler function, check whether the request came in from your form. *stop talking already, give us the code!!*
One tiny other thing: Using REQUEST_URI for your action doesn’t cater for the fact that it could contain several other query arguments which you’d be passing on
Ok, now, the code: Point your form’s action to a plugin menu page:
<form action="<?php echo admin_url('admin.php?page=my-plugin-page');?>" method="POST"> <!--The rest of your input fields--> <input type="submit" value="Submit" name="my-submit" />
NB: I found that my form defaults to GET hence the need to explicitly define method=”POST”
Then to handle the form submission, put this in the constructor of the class that’s going to handle your data:
add_menu_page($page_title, $menu_title, $capability, "my-plugin-page", array($this,handle_form_submission)); public function handle_form_submission(){ if ( isset($_POST['my-submit']) ) { //Handle your form's data } else{ //Output the my-plugin page HTML } }
Note that you don’t necessarily have to define a new plugin menu page just to handle your form submission; you can re-use one you’ve already defined-all you need is to add the if ( isset($_POST[‘my-submit’]) ) to pick-up your data and proceed with processing.
Don’t forget to add a nonce to your form.