How to submit a form in wordpress without plugin?

In this post we will teach you how to submit a form in wordpress without plugin.

In wordpress form is a module through which we collects or shares some information from users on our website or blog.

There are many types of form in wordpress like:

  • Newsletter Form
  • Subscription Form
  • Membership Form
  • Contact Form or Query Form
  • Checkout Form For E-Commerce
  • Booking Form
  • Application Form
  • Get a Quote or Estimation Form
  • Lead Generation Form
  • Admission Form For Education Websites
  • Chat Form or Ticket System Form
  • Email Subscriber Form or Sharing Form

From the list given above there can be more types of forms in website. In WordPress we can create as many types of forms we want for any purposes on our website or blog.

Create and Submit a Form in WordPress With Plugin

If you want to submit or create a form in wordpress with plugin. Then you can use the wordpress plugins given below.

Submit a Form in WordPress Without Plugin

If you want to submit or create a form in wordpress without plugin. Then follow the steps given below. Here we will create a simple contact form. You can create as many forms by following this article.

Step 1

Create a custom template page in your themes directory located at website_root_path/wp-content/themes/your_theme. You can read our article how to create a custom post template in wordpress. Here below we have created a template page Contact Form and created a form in this template. So, simply create a file contact-template.php file in your themes directory and paste the code given below in the file.

<?php
/*
 * Template Name: Contact Form Template
 * Template Post Type: page
 */

get_header(); 

?>

<?php if(isset($_GET['success'])): ?>
	<div class="alert alert-success">
		<h3>Congrats! Your Form Submitted Successfully.</h3>
	</div>
<?php endif; ?>

<?php if(isset($_GET['error'])): ?>
	<div class="alert alert-danger">
		<h3>Sorry! Unable to submit the form.</h3>
	</div>
<?php endif; ?>

<form name="contact_form" method="POST" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" enctype="multipart/form-data" autocomplete="off" accept-charset="utf-8">

	<div>
		<label>
			Full Name
			<input type="text" name="contact_full_name" required="">
		</label>
	</div>

	<div>
		<label>
			Email
			<input type="email" name="contact_email" required="">
		</label>
	</div>

	<input type="hidden" name="action" value="contact_form">

	<input type="hidden" name="base_page" value="<?php echo home_url( $wp->request ); ?>">

	<div>
		<button type="submit" name="submit_btn">
			Submit
		</button>
	</div>

</form>
<!-- new registeration -->

<?php

get_footer();

?>

Now we have a contact form template page. Go to your WordPress Dashboard and create a page and select your template Contact Form Template from the right sidebar Page Attributes. After creating a page open your page url and you will the see the form we have created in our Contact Form Template page.

Step 2

Now we will submit the form given above for this just paste the code given below in your functions.php file located in your themes directory.

add_action( 'admin_post_nopriv_contact_form', 'process_contact_form' );

add_action( 'admin_post_contact_form', 'process_contact_form' );

function process_contact_form(){

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	GLOBAL $wpdb;

	$params = $_POST;

        $params['contact_full_name'] = preg_replace("/[^a-zA-Z ]+/", "",filter_var(strip_tags($params['contact_full_name']), FILTER_SANITIZE_STRING));
        $params['contact_email'] = filter_var(strip_tags($params['contact_email']), FILTER_SANITIZE_EMAIL);

	/*create table if not exists*/

	$table_name = $wpdb->prefix.'custom_contact_form';

	$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) );

	if ( ! $wpdb->get_var( $query ) == $table_name ) {

		$sql = "CREATE TABLE {$table_name} (
		id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
		contact_full_name VARCHAR(255) NOT NULL,
		contact_email VARCHAR(255) NOT NULL,
		created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
	)";

	if($wpdb->query($sql)){
		submitsForm($table_name,$params);
	}


}else{
	submitsForm($table_name,$params);
}

/*create table if not exists*/

die;

}

}

function submitsForm($table_name, $params){

	GLOBAL $wpdb;

	$curTime = date('Y-m-d H:i:s');

	$query = "INSERT INTO {$table_name}(contact_full_name, contact_email,created_at) VALUES('{$params['contact_full_name']}','{$params['contact_email']}','{$curTime}')"; 

	if($wpdb->query($query)){
		wp_redirect($params['base_page'].'?success=1'); 
	}else{
		wp_redirect($params['base_page'].'?error=1'); 
	}
}

That’s it Now submit your form and check your database table (table_prefix)custom_contact_form, this table will have your submitted data.

Code Explanation

  • In Template we have used the Template Name Contact Form Template and Template Post Type Page.
  • In Form Action we have used the admin_url(‘admin-post.php’) to get the location of form handling wordpress core file.
  • We have created a two hidden field in our form and important hidden field is action contact_form which we have used in our functions.php file.
  • We used the wordpress function add_action which hooks our function process_contact_form.
  • In add_action we used two hooks admin_post_(for logged in users) and admin_post_nopriv_(for not logged in users) and added our action-contact form created in hidden form field action.
  • We have used $wpdb global wordpress database object to run database query.

So, Using these tricks you can create a custom forms in wordpress without plugin, create a newsletter forms in wordpress without plugin, create a lead generation form in wordpress without plugin or any custom contact form in wordpress without plugin.

If you want more tutorials and tricks about wordpress then visit our WordPress Page and follow us on facebooktwittertumblrlinkdedin and if you like this article then share this.