How to create WordPress plugins Part 2: Working with data and Forms

This is part 2 of my tutorial series on How to create WordPress plugins.

first let us create our database table. this will be the table holding the data that will be saved from the form that we will be creating.

execute the following Mysql command on your wordpress database to create the table.

CREATE TABLE IF NOT EXISTS `wp_contactf` (
  `contactid` bigint(10) NOT NULL AUTO_INCREMENT,
  `c_name` char(40) NOT NULL,
  `c_email` varchar(40) NOT NULL,
  `c_message` text NOT NULL,
  PRIMARY KEY (`contactid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

take note that the prefix wp_ on the table name wp_contactf may vary depending on your wordpress installation. apply the prefix accordingly.

after the database table creation, create a directory on your wp-content/plugins named contactform and inside it contactform.php

on your contactform.php put in this code as our starter file.

<?php
/*
Plugin Name: Sample contactform
Plugin URI: http://www.ombing.com
Description: sample of a simple contact form
Author: your name
Version: 1.0
Author URI: http://www.ombing.com/
*/

function contactform($content=”){

}

add_filter(‘the_content’, ‘contactform’);

?>

let us assign a short code for showing the contact form. “show_contact_form”.

and modify the contactform function as follows:

function contactform($content=”){
     $shortcode=”show_contact_form”;

     if(!preg_match(‘|['.$shortcode.']|’, $content)) {  
           return $content;
     }#preg_match

     $sdstr = “”;
     if(strstr($content, “[".$shortcode."]“)) {
           $sdstr .= ccf_show_contactform();
           $content = str_replace(‘['.$shortcode.']‘, $sdstr, $content);
     }#if shortcode

     return $content; 
}#contact form

what we did here was we specified a shortcode to show our form “show_contact_form” and if that shortcode exists on the content call up a function ccf_show_contactform() with the following line:

$sdstr .= ccf_show_contactform();

needless to say lets create that function… and put in an html code to show the form.

function ccf_show_contactform(){
$sdstr =’
    <form cellpadding=”2″ cellspacing=”2″ method=”post”>
    <p>
    <label Name:</label><BR>
    <input type=”text” name=”c_name” id=”c_name” size=40>    </p>

    <p>
    <label>Email:</label><BR>
    <input type=”text” name=”c_email” id=”c_email” size=40>   </p>
    <p>
    <label>Message:</label><BR>
    <textarea name=”c_message” id=”c_message” cols=40 style=”vertical-scroll=yes;”></textarea>    </p>

    <p>
    <input type=”hidden” name=”isposted” id=”isposted” value=”1″>
    <input type=”submit” name=”submit” id=”submit” value=”Save Message”>     </p>

    </form>
    ’;
    return $sdstr;

}#function ccf_show_contactform

now, have your plugin activated and add the [show_contact_form] shortcode. on one of your pages then navigate to that page to view what we have done so far.

now let us work on the function that would be saving our form data to the database. let us create a function named ccf_save_data();

function ccf_save_data($postdata){
    $sdstr = “”;
    global $wpdb;
    $tblname=$wpdb->prefix.”contactf”;
    $rows_affected = $wpdb->insert( $tblname, array( ‘c_name’ => $post['c_name'], ‘c_email’ => $post['c_email'], ‘c_message’ => $post['c_message']);
    if($rows_affected > 0){
        $sdstr .= “Your message was successfully recorded.”;    
    }#rows affected
    return $sdstr;
}#function ccf_save_data

$wpdb is the global wordpress database class that handles the interface between the site and its database.. to read more about it click here.

and add the following code on our contactform function below the $sdstr .= ccf_show_contactform(); line.

       if(isset($_POST["isposted"]) && $_POST["isposted"]==1){
             $sdstr .= ccf_save_data($_POST);
        }

test out your code. you would be able to have your data saved now to your database. To check your data use whatever mysql client(like phpmyadmin) that you like.

 


MySQL, PHP, Wordpress

Leave a Reply