Professional Documents
Culture Documents
Shibu Lijack
Forms are an integral part of a website. While there are lots of wordpress plugins available to create custom
forms, most of them are not up to the task. It is very easy to create a form in WordPress, provided you have
a fair knowledge of php, html and js. Let me show you how its done.
STEP #1
Front-end [HTML]
https://shibulijack.wordpress.com/2012/03/18/create-custom-forms-in-wordpress/ 1/3
9/1/2017 How to create custom forms in WordPress without using plugins? Shibu Lijack
> Specify the backend php script in the action attribute of the form tag. Include as many form fields as you
wish. Specify the method of form submission as post.
Bravo! You have successfully completed the front end of the custom form.
STEP #2
1 <script type="text/javascript">
2 /* JS validation code here */
3 ...
4 ...
5 </script>
> Create a validation function:
1 function validateForm()
2 {
3 /* Validating name field */
4 var x=document.forms["myForm"]["name"].value;
5 if (x==null || x=="")
6 {
7 alert("Name must be filled out");
8 return false;
9 }
10 /* Validating email field */
11 var x=document.forms["myForm"]["email"].value;
12 var atpos=x.indexOf("@");
13 var dotpos=x.lastIndexOf(".");
14 if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
15 {
16 alert("Not a valid e-mail address");
17 return false;
18 }
19 }
https://shibulijack.wordpress.com/2012/03/18/create-custom-forms-in-wordpress/ 2/3
9/1/2017 How to create custom forms in WordPress without using plugins? Shibu Lijack
STEP #3
Back-end [PHP]
1 <?php
2 //get the form elements and store them in variables
3 $name=$_POST["name"];
4 $email=$_POST["email"];
5 ?>
1 <?php
2 //establish connection
3 $con = mysqli_connect("Host","User name","Password"
4 //on connection failure, throw an error
5 if(!$con) {
6 die('Could not connect: '.mysql_error());
7 }
8 ?>
1 <?php
2 $sql="INSERT INTO `DB name`.`Table name` ( `name` , `email_id` ) VALUES
3 mysqli_query($con,$sql);
4 ?>
> After the database is successfully updated, you need to redirect the user to a page with a success
message(which I have assumed you have created via Dashboard). You can do this by,
1 <?php
2 //Redirects to the specified page
3 header("Location: http://your-success-page-url
4 ?>
Now you have successfully created your php script which will be called in the action attribute. Upload this
php file inside the wordpress directory.
NOTE: In the form action, I have used ../process.php because the php file is one level above the page
which contains the form.
STEP #4
That is it! You have successfully created a custom form and connected it with a database!
https://shibulijack.wordpress.com/2012/03/18/create-custom-forms-in-wordpress/ 3/3