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 ?
- Create a form anywhere in your website.
- 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
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
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
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.
Leave a Reply