X

How to create custom forms in WordPress without using Plugins


How to create custom forms in WordPress without using Plugins? . The question comes, when you create a theme for your own. But here I have a simple tut for you to code it easily.  So lets get into the topic to create form on your page to get feedback from your site visitors.

html-forms

Open your Themes directory and open your current active theme files. Than copy “page.php ”  and rename it as “kv-feedback.php“. Than open the file on a text editor. Here we gonna play and make a new form and collect reviews from them .

just add the following line of code to add your custom file(kv-feedback.php) to work with wordpress page. Just add the following line of code before the “get_header()” . I mean add it below the “<?php ” as like the following code.

<?php 
/** 
Template Name : Feedback form
*/
get_header();

Save the file and upload to the host again and move to wordpress admin Pages->Add New . Here you have to name your page and on the right end of the window, you can see the option to choose templates. There Select Feedback form from the Drop down menu and publish the page.

Now, view the page. The page looks empty with header only. So new we are ready to write form works. Its simple to create a form, just write the following html code to show a form to the users.

<form name="FindAPlate" id="FindAPlate" method="post" action="" >
 <h3 class="cf_text">Your Feedback</h3>
	<table name="form_table_find_aPlate" >
		<tr> <td>Name*</td><td> <input  maxlength="150" size="30" title="" id="sws_persion_name" name="sws_persion_name" type="text" /></td> </tr>
		<tr> <td>Contact Number*</td><td> <input  maxlength="150" size="30" title="" id="sws_persion_contact_no" name="sws_persion_contact_no" type="text" /></td> </tr>
		<tr> <td>Email*</td><td> <input  maxlength="150" size="30" title="" id="sws_persion_email" name="sws_persion_email" type="text" /></td> </tr>
		 <tr> <td>Comments</td><td><?php $kv_editor_args =  array('media_buttons' => false , 'teeny' => true ); wp_editor( '', 'sws_persion_extra_info',  $kv_editor_args ); ?></td> </tr>
		<tr>  <td colspan="2" style="text-align: center;"><input type="hidden" name="action" value="store_feedback" > <input value="Submit" name="button_9" type="submit" /> </td>  </tr>
	</table> 
</form>

Here we made a form to show the form to users and ask them to fill the data.  Now, we need to retrieve the form data and validate it and store it on db. So the above form i used Post method. So your  PHP code looks like this.

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "store_feedback") {

}

And , its important to verify the user entered content on the form. here is a simple php escaping functions.

$fields = array(
				'sws_persion_name',
				'sws_persion_contact_no',
				'sws_persion_email',
				'sws_persion_feedback'				
			);
	foreach ($fields as $field) {
		if (isset($_POST[$field])) $posted[$field] = stripslashes(trim($_POST[$field])); else $posted[$field] = '';
	}

And now, you need to store the values on the database table.  with help of $wpdb object like the following example.

global $wpdb; 
$tbl_name = $wpdb->prefix.'feedback'; 
$kv_data = array( 
	'person_name' => $sws_persion_name, 
	'contact_no'	=> $sws_persion_contact_no, 
	'email'			=> $sws_persion_email,
	'feedback' 	=> $sws_persion_feedback 
) ; 		
$wpdb->insert( $tbl_name, $kv_data )

The above one will update the user feedback’s and things into it.  Here is the complete code for your reference.

<?php
/**
Template Name: Find A Plate
*/
get_header();

global $wpdb; 
$tbl_name = $wpdb->prefix.'feedback'; 
$errors = new WP_Error();	
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "submit-a-plate") {
	$fields = array(
				'sws_persion_name',
				'sws_persion_contact_no',
				'sws_persion_email',
				'sws_feedback'				
			);
	foreach ($fields as $field) {
		if (isset($_POST[$field])) $posted[$field] = stripslashes(trim($_POST[$field])); else $posted[$field] = '';
	}
	
	if ($posted['sws_persion_name'] != null )
		$sws_persion_name =  $_POST['sws_persion_name'];
	else 
		$errors->add('empty_title', __('<strong>Notice</strong>: Please enter your name first.', 'sws'));		
	
	if ( !$errors->get_error_code() ) { 
	$kv_data = array( 
					'person_name' => $sws_persion_name, 
					'contact_no'	=> $sws_persion_contact_no, 
					'email'			=> $sws_persion_email,					
					'feedback' 	=> $sws_feedback 
				) ; 
		
		if ($wpdb->insert( $tbl_name, $kv_data ) ) {
            	 $sub_success ='Success' ;
		}
		if ($sub_success == 'Success' ) {
			unset($sws_persion_name, $sws_persion_contact_no, $sws_persion_email, $sws_persion_registration, $sws_persion_no_plate_wanted, $sws_persion_extra_info, $sws_persion_budget );
		}
	}
}

?>


<div class="padding_article">

<div class="article-content" style="line-height: 3em;">
<?php 
	if($sub_success == 'Success') {
		echo '<div class="success">' . __( 'Thank you we will get back you soon.', 'post_new' ) . '</div>';
		$sub_success = null;
	} 
	if (isset($errors) && sizeof($errors)>0 && $errors->get_error_code()) :
		echo '<ul class="errors">';
		foreach ($errors->errors as $error) {
			echo '<li>'.$error[0].'</li>';
		}
		echo '</ul>';
	endif; 	
?>
<h2>Feedback form</h2>

<span style="color: #000000;">If you are searching for a plate and it is not on our list, then fill in the form below. We will then search our extensive resources and endevour to get it as soon as we can. 			
 <form name="FindAPlate" id="FindAPlate" method="post" action="" >
 <h3 class="cf_text">Your Details</h3>
	<table name="form_table_find_aPlate" >
		<tr> <td>Name*</td><td> <input  maxlength="150" size="30" title="" id="sws_persion_name" name="sws_persion_name" type="text" /></td> </tr>
		<tr> <td>Contact Number*</td><td> <input  maxlength="150" size="30" title="" id="sws_persion_contact_no" name="sws_persion_contact_no" type="text" /></td> </tr>
		<tr> <td>Email*</td><td> <input  maxlength="150" size="30" title="" id="sws_persion_email" name="sws_persion_email" type="text" /></td> </tr>
		 <tr> <td>Feedback</td><td><?php $kv_editor_args =  array('media_buttons' => false , 'teeny' => true ); wp_editor( '', 'sws_feedback',  $kv_editor_args ); ?></td> </tr>
		<tr>  <td colspan="2" style="text-align: center;"><input type="hidden" name="action" value="submit-a-plate" > <input value="Submit" name="button_9" type="submit" /> </td>  </tr>
	</table> 
</form>
									
<?php 	get_footer();	?>

 

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, OpenCart Theme Development, PHP, HTML, CSS, jQuery, Bootstrap development and content SEO.

9 comments

  1. commenter

    thank for nice tutorail.but i have a question when i submit the form only name of person is inserted into tables.other fields are empty.so i think there is some error in the script.please let me know.how to fix this issue.thanks once agian.

  2. commenter

    prefix.’feedback’;
    $errors = new WP_Error();
    if( ‘POST’ == $_SERVER[‘REQUEST_METHOD’] && !empty( $_POST[‘action’] ) && $_POST[‘action’] == “submit-a-plate”) {
    $fields = array(
    ‘sws_persion_name’,
    ‘sws_persion_contact_no’,
    ‘sws_persion_email’,
    ‘sws_feedback’
    );
    foreach ($fields as $field) {
    if (isset($_POST[$field])) $posted[$field] = stripslashes(trim($_POST[$field])); else $posted[$field] = ”;
    }

    if ($posted[‘sws_persion_name’] != null )
    $sws_persion_name = $_POST[‘sws_persion_name’];
    else
    $errors->add(’empty_title’, __(‘Notice: Please enter your name first.’, ‘sws’));

    if ( !$errors->get_error_code() ) {
    $kv_data = array(
    ‘person_name’ => $sws_persion_name,
    ‘contact_no’ => $sws_persion_contact_no,
    ’email’ => $sws_persion_email,
    ‘feedback’ => $sws_feedback
    ) ;

    if ($wpdb->insert( $tbl_name, $kv_data ) ) {
    $sub_success =’Success’ ;
    }
    if ($sub_success == ‘Success’ ) {
    unset($sws_persion_name, $sws_persion_contact_no, $sws_persion_email, $sws_persion_registration, $sws_persion_no_plate_wanted, $sws_persion_extra_info, $sws_persion_budget );
    }
    }
    }

    ?>

    <?php
    if($sub_success == 'Success') {
    echo '’ . __( ‘Thank you we will get back you soon.’, ‘post_new’ ) . ”;
    $sub_success = null;
    }
    if (isset($errors) && sizeof($errors)>0 && $errors->get_error_code()) :
    echo ”;
    foreach ($errors->errors as $error) {
    echo ”.$error[0].”;
    }
    echo ”;
    endif;
    ?>
    Feedback form

    If you are searching for a plate and it is not on our list, then fill in the form below. We will then search our extensive resources and endevour to get it as soon as we can.

    Your Details

    Name*
    Contact Number*
    Email*
    Feedback false , ‘teeny’ => true ); wp_editor( ”, ‘sws_feedback’, $kv_editor_args ); ?>

  3. commenter

    I use the same code you have provided and make just a database table with fields.

    • commenter

      This is not proper formating. The code has missing some syntax,

      can you use `< pre >` tag and update the code again. so i can get the right code. Now its missing. and not formatted properly.

      • commenter

        yes there is missing when i copy code in message box it remove the tags.
        but i used same code you have provided in the tutorial and create a table name with feedback with columns.
        but only name field is insert with data.all rest of the column are empty.

  4. commenter

    Hello give me a favor if you can give me your email so i will email my files to you.

  5. commenter

    Hi Varadharaj,

    I am designer, not much into the coding. My client has two pages on his static + php website, which has only two or three pages coded in php and uses just two simple mysql tables. One page displays his books and the other displays some of his clients (this has a year wise filter). Both have lot of similar fields. A logo, four to five fields.

    First the clients page: Logo and three lines of text.

    The Books page: Has a logo, Title, 3-4 lines of text and a date. Plus a webpage link and a pdf link. These books have a yearly filter as well.

    Now he wants a WP site, I want a simple plugin where i can enter these details and display them.

    Any help is appreciated.

Comment Below

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

*

Current ye@r *

Menu

Sidebar

Subscribe E-mail Updates

Subscribe to our email newsletter.

 

Advertisement

Category Posts