How to Submit a Plugin Form to Another Page

In WordPress plugin development, the question often arises of how to properly submit a form to another page in your plugin directory. Say for example you are creating a plugin called Admin Notes that allows you to leave notes in the wp-admin dashboard. In this plugin you have a main directory folder named “jafty-admin-notes” that contains the jafty-admin-notes.php file, a sub-directory named “pics” and a sub-directory named “inc”. Inside “inc” you have a file named “form.php” that has the form which submits to another page in “inc” named “form-processor.php”. Finally there’s an image in “pics” named “trash.gif” Which you can copy from here:

trash can gif image

(The above image is actually 16px by 16px but is enlarged here so you can easily click on it to save it to your desktop)

Here is what your admin notes plugin will look like when finished:

jafty admin notes plugin image

Below is what it looks like when it has submitted to a new page which while it doesn’t appear to be in the wp-admin, it does include all the WordPress capabilities so you can use WordPress classes, functions and other WordPress objects in your PHP code. This is the DELETE process in a new page:

delete note image

Both the delete and add new note process are both performed in the same new page in a script named “form-processor.php” inside the inc folder as explained above.

THE CODE:

First, lets look at the main plugin file which is jafty-admin-notes/jafty-admin-notes.php. Here’s the code:

<?php
/**
 * Plugin Name: Jafty Admin Notes
 * Plugin URI: http://jafty.com/blog/how-to-submit-a-plugin-form-to-another-page/
 * Description: A Plugin created for leaving admin notes inside of the wp-admin dashboard. Email Ian L. of Jafty.com for more info at: linian11@yahoo.com
 * Version: 1.00
 * Author: Ian L. of Jafty.com
 * Author URI: http://jafty.com
 * License: GPL2
 * Created on: 10-26-2014
 * Updated on: 10-26-2014
 */
 
function install_tbl () {
global $wpdb;
    $table_name = $wpdb->prefix . ‘notes’;

$sql = “CREATE TABLE $table_name (
  id int(9) NOT NULL AUTO_INCREMENT,
  time datetime DEFAULT ‘0000-00-00 00:00:00’ NOT NULL,
  content varchar(999) DEFAULT NULL,
  UNIQUE KEY id (id))”;

    require_once( ABSPATH . ‘wp-admin/includes/upgrade.php’ );
    dbDelta( $sql );

require_once( ABSPATH . ‘wp-admin/includes/upgrade.php’ );
dbDelta( $sql );

//Now add some data to the table:
$field_content = ‘this is a test note’;

$wpdb->insert(
    $table_name,
    array(
        ‘time’ => current_time(‘mysql’),
        ‘content’ => $field_content,
    )
);

}//end install_tbl function

//call install_tbl function when plugin is activated by admin:
register_activation_hook( __FILE__, ‘install_tbl’ );

function pluginUninstall() {

        global $wpdb;
        $table = $wpdb->prefix.”notes”;

        //Delete any options thats stored also?
    //delete_option(‘wp_yourplugin_version’);

    $wpdb->query(“DROP TABLE IF EXISTS $table”);
}//end pluginUninstall function

//hook into WordPress when its being deactivated:
register_deactivation_hook( __FILE__, ‘pluginUninstall’ );

//Add admin menu item to wp-admin:
add_action(‘admin_menu’,’add_notes_menu’);

function add_notes_menu(){
add_menu_page(‘Jafty Admin Notes’, ‘Jafty Admin Notes’, ‘manage_options’, ‘jafty_admin_notes’, ‘create_admin_notes_pg’,”,4);
//add_submenu_page(‘jafty_super_importer’,’Notes’, ‘Notes’,’manage_options’, ‘manage_notes_slug’, ‘create_notes_pg’);
}//end add_notes_menu function.

function create_admin_notes_pg(){
include “../wp-content/plugins/jafty-admin-notes/inc/form.php”;
}//end create_admin_notes_pg function

//allow form.php to submit to different php script:
add_action(‘admin_post_submit-form’, ‘_handle_form_action’); // If the user is logged in
add_action(‘admin_post_nopriv_submit-form’, ‘_handle_form_action’); // If the user in not logged in
function _handle_form_action(){

include ‘inc/form-processor.php’;

}
?>

And here is the jafty-admin-notes/inc/form.php script:

<h1>Jafty Admin Notes</h1>
<p>A plugin created by Ian L. of <a href=”http://jafty.com”>Jafty.com</a> that allows wp-admin dashboard users to leave notes in the administration area of WordPress</p>
<form action=”<?php echo get_admin_url();?>admin-post.php” method=’post’>
<input type=’hidden’ name=’action’ value=’submit-form’ />
Note:<br />
<textarea name=”note”></textarea>
<input type=”submit” name=”sbmt” value=”Save Note” />
</form>
<hr />
<h2>Saved Notes:</h2>
<?php
global $wpdb;
$table_name = $wpdb->prefix.”notes”;
$myquery =     “SELECT * FROM $table_name”;

$allnotes = $wpdb->get_results($myquery);

$i=1;
foreach($allnotes as $cur_note) {
$cid = $cur_note->id;
$ctime = $cur_note->time;
$cnote = $cur_note->content;
?>
<form style=”display:inline;position:relative” action=”<?php echo get_admin_url();?>admin-post.php” method=’post’>
<input type=’hidden’ name=’action’ value=’submit-form’ />
<input type=’hidden’ name=’del_id’ value='<?php echo $cid; ?>’ />
<input style=”position:relative;top:6px” type=’image’ src=’../wp-content/plugins/jafty-admin-notes/pics/trash.gif’ />
</form>
<?php
echo “$i)$ctime – $cnote<hr />”;
$i++;
}//end foreach note.
?>

And finally, the jafty-admin-notes/inc/form-processor.php script:

<?php
//form-processor.php
global $wpdb;
$table_name = $wpdb->prefix.”notes”;

if(isset($_POST[‘del_id’])){//if it is a delete request:
$did = $_POST[‘del_id’];
echo “Note ID# $did is being deleted.<br />”;
$wpdb->delete($table_name, array(‘id’ => $did));
?>
<hr />
You are now being redirected to the admin notes page…
<script>
window.location.href=”http://jafty.com/blog/wp-admin/admin.php?page=jafty_admin_notes”;
</script>
<?php
}else{//else, must be new note:
$note = $_POST[‘note’];

$wpdb->insert(
    $table_name,
    array(
        ‘time’ => current_time(‘mysql’),
        ‘content’ => $note,
    )
);

echo “The following NOTE has been entered into the wp_notes table:<br>$note<br>”;
?>
<hr />
You are now being redirected to the admin notes page…
<script>
window.location.href=”http://jafty.com/blog/wp-admin/admin.php?page=jafty_admin_notes”;
</script>
<?php
}//end else must be new note
?>

Summary

I know I did not include much explanation above, but I hope to add more later as I am out of time. The code does work and was well tested, so it should speak for itself. Study the code and learn to implement your own plugin that submits to another page. The key is in the first script above, so pay attention to the last half a dozen or so lines of code. That is the key part in making the submit to page functionality work.

One Reply to “How to Submit a Plugin Form to Another Page”

  1. I would like to kindly and highly request that you please, please explain the code in more detail so as to understand the connection between the forms.

    I am facing a similar issue whereby a user has to submit donation data to a payment processing form from which a user can confirm details and then submit the processed form to the payment gateway. in normal php , implementation is a piece of cake but I am highly frustrated with the implementation on wordpress. all down to knowing how the field is handled.

    You can reach me on email to discuss further

Leave a Reply

Your email address will not be published. Required fields are marked *