Store a custom form user input into WordPress database using Php

Pranavan Sivarajah
3 min readDec 4, 2016

--

Insert user input in a database using the form in WordPress. Customs form that store input in the database. What if you already have a database…

Insert data in a database using the form in WordPress. Customs form that store input in the database. What if you already have a database of say, request information table, but you want to be able to save that data into a database within a WordPress template? Today I’ll be showing you just how to do that, safely within the WordPress engine.

Requirements

  • Your own self-hosted WordPress site, obviously.
  • Basic PHP and MySQL skills
  • An existing dataset in MySQL.

What Are We Going to Do Before Coding?

Create a relevant table on the database, I have created a table like below (MySQL Code)

CREATE TABLE `request` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(255) NOT NULL,
`Email` varchar(255) DEFAULT NULL,
`Service` varchar(25) DEFAULT NULL,
`Date` date DEFAULT NULL,
`Address` varchar(255) DEFAULT NULL,
`Phone` int(11) DEFAULT NULL,
`Message` varchar(100) DEFAULT NULL,
`status` varchar(10) NOT NULL DEFAULT 'start',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

Making a Custom Template

If you want to use some of your own PHP code, the easiest way to do this is to create a custom template, then apply the template to a particular page you create in WordPress. Start by opening up your theme files and duplicating the page.php (or single.php if there isn’t one).

You can find the page.php file in wp-content\themes\<<Your Theme Name>> then duplicating. Rename it something obvious, like “Request.php” as I’ve chosen.
At the very top of the file, we need to tell WordPress this is a custom template. Do this by adding the following (this is a PHP-style comment, so it should be after any opening PHP tag if present).

/*Template Name: Request Page
*/

Obviously, call it whatever you like

Now, find the main content function. You can delete it if you want, but I’m just going to add the extra code after it. With the default twenty-fifteen theme, you’re looking for:

Here I have created a template according to my table “request’:
Request.php

Then we’ll need to create a page on the WordPress admin page, and apply our page template to it.

Publish, and check out the page to see if your form has worked.

Yeah! Now you can Submit data through this form, it will insert into the request table. 🥳🥳🥳

Form Validation / Required

Here I have demonstrated how basically store data into a table using the form if you have to validate every field and make it as required, etc install the Wp plugin Contact form 7, Then use these classes in your form.
Ref links All classes

example:

class = "wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email" aria-required = "true" aria-invalid = "false"

I hope this is useful to some of you in your WordPress projects. Next, I’ll be tackling the slightly trickier topic of how to view database tables’ data in your admin dashboard. WordPress backend customization.

If you have any questions please post your comments below. 🙏

Originally published at https://www.bethedev.com on December 4, 2016.

--

--

Pranavan Sivarajah

iOS Developer. Been into music. I enjoy playing music 🎸. Freelancer. Blogger. SwiftUI | Objective C | Swift | | UIKit | Combine | RxSwift | iOS