How to handle form in WordPress plugin

I am stuck in form handling while developing WordPress Plugin. I face two problems and after Googling found some solutions to handle form in the WordPress plugin.

There are three ways to handle form in WordPress Plugin.

  1. Traditional HTML method passing your script name in action atrribute e.g. action=”form_save.php”
  2. Using admin-post hook
  3. Submit form using Ajax.


In the core PHP form handling the form look like

<form name="Form1" action="form_save.php">
<input name='text" value=""/>
<input name="submit" value="submit"/>

Now, here is the problem how to get the path of the script I develop which is saved in my WordPress plugin folder i.e. “form_save.php”

This can be achieved by the built-in WordPress function plugins_url. This function retrieves the absolute URL to the plugins

<form action="<?php echo plugins_url( 'form_save.php', __FILE__ ); ?>" method="post" >
<input name='text" value=""/>
<input name="submit" value="submit"/>


Before starting the admin-post.php method of submitting form request, check the form below

<form action="<?php echo admin_url( 'admin-post.php'); ?>" method="post" >
<input name='text" value=""/>
<input name='action' value='form_handle'/>
<input name="submit" value="submit"/>

You can use this hook to implement custom handlers for your own GET and POST requests. The admin post_ hook has the format “admin post $action,” where $action is the ‘action’ argument of your GET or POST request.

add_action( 'admin_post_form_save', 'form_save' );
function form_save() {
  die(); // must exit 

Here when the user clicks on submit button, It will call the admin-post.php generic function whose action is defined using add_action (‘admin_post_form_save’, ‘form_save’). This will call the form_save function.

And all the form handling is done in the form_save.php script which is included in the function.


the job is not done. Here are some questions which I face too.

How to redirect back to the WordPress Plugin page?

Once the form_save script does its work, the next step is how to redirect back to the plugin page. Using the WordPress wp_redirect function we can redirect back to the plugin page.




How to pass parameters to the WordPress plugin page?

In Form handling validation can be done at javascript. But, in my case, I have handled validation at the server. And, if there is an error it must return to the plugin page. The challenge is to pass parameters to the plugin page.




Simple, using the passing value in the query string and I can get the value on my plugin page.