CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   WordPress   WordPress Form to Database Tutorial – How to Insert Da ...

WordPress Form to Database Tutorial – How to Insert Data into database Via Form ?

September 1, 2018 by SNK

wordpress insert into database via form

In this WordPress form to database tutorial we are going to learn how to insert data into WordPress database via form using wp_insert_post(); and insert data into your custom post type that is visible in the WordPress admin dashboard.

The WordPress function wp_insert_post(); accepts parameter in the form of an array. If you want to look what are the parameters that it accepts, you can click this link to the WordPress documentation and know more about it.

As a part of this tutorial we are just going to cover most basic part and that is post_title, post_description, post_date, post_status and finally post_type which will be needing in this tutorial.

Lets get started.

What are we going to do ?

  1. Create a form anywhere in your website.
  2. Write the code in functions.php to grab the inserted data via form and insert it into database.

Step 1:- Creating a form to Insert Data into Database Via Form

HTML Form
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<div class="container">
<h1>WordPress Insert Post Via Form (Demo)</h1>
<form action="#" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="">Enter Title</label>
<input type="text" class="form-control" name="post_title" placeholder="Enter Title" />
</div>
<div class="form-group">
<label for="">Enter Description</label>
<textarea name="post_description" id="" cols="30" rows="10" class="form-control" placeholder="Enter Post Description"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit_enquiry_form" class="btn btn-primary"><i class="glyphicon glyphicon-pencil"></i> Submit</button>
</div>
</form>
</div>

 

Make sure that button type of the form should be submit and also the name of the button should be very unique and identical to what are you going to insert, which in my case in “submit_enquiry_form”

We are going to use the name of button in the next step to grab the value of the form and insert it into WordPress database.

Step 2 :- Using WP_INSERT_POST to Insert Form Data into Database

WordPress Insert Query
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if(isset($_POST['submit_enquiry_form'])) {
 
    $title = $_POST['post_title'];
    $content = $_POST['post_description'];
 
    $args = [
            'post_title'=> $title,
            'post_description'=>$content,
            'post_status'=> 'publish',
            'post_type'=>'post', // name of your custom post type
            'post_date'=> get_the_date()
    ];
 
    $is_post_inserted = wp_insert_post($args);
 
    if($is_post_inserted) {
        wp_redirect(wp_get_referer());
    }
}

If you the copy the code exactly it will work beside its design, We are using bootstrap here to quickly make the design and get started quickly.

If you want to send the custom message after the form have been inserted. You can customize the redirection after the insert function using wp_redirect() as i used in the above code. Just make the necessary changes like below

Redirecting Back to Reefer with Message
PHP
1
2
3
4
5
if($is_post_inserted) {
        wp_redirect(wp_get_referer()."?message=success");
    } else {
        wp_redirect(wp_get_referer().."?message=failed");
    }

wp_get_reefer() will track the page where you were currently in before submitting a form and will redirect it back to with a message. Later you can use $_GET['message'] depending upon success or fail of the result and print out the message.

This is it. If you have any problem inserting on this how to save form data in database in WordPress you can contact us via comments or contact us through contact form.

SHARE ON
Buffer

Enjoyed this article?

Like us on

WordPress how to save form data in database in wordpress how to save form data into wordpress database wordpress form to database tutorial wordpress insert data into database

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

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

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.