Hello guys. I am showing you how to add custom post type form frontend and show data backend.
1) Create a Custom Post type to add the data.(Ex: blog posts)
2) Display a form to submit user data.
3) Data post fields and content will added to database.
User can submit content by form from frontend. The form will use fields post title, post content, post categories, post_tags etc….
Check below code : Create form in frontend
<div class="container"> <div id="wrap"> <form action="" method="post"> <table border="0" width="100%"> <tr> <td><label for="post_title">Post Title</label></td> <td><input name="post_title" type="text" /></td> </tr> <tr> <td><label for="post">Post Content</label></td> <td><textarea id="post" tabindex="3" name="post" cols="50" rows="6"></textarea></td> </tr> <tr> <td><label for="category">category</label></td> <td><?php wp_dropdown_categories( 'hide_empty=0' ); ?></td> </tr> <tr> <td><label for="post_tags">Tags</label></td> <td><?php wp_dropdown_categories(array('taxonomy'=> 'post_tag','hide_empty' => 0, 'name' => 'my_tags'));?></td> </tr> <tr> <td></td> <input name="submit" type="submit" value="submit" /> </tr> </table> </form> </div> </div>
wp_insert_post function to user insert of post into the database.
Form submit code:
<?php $postTitle = $_POST['post_title']; $post = $_POST['post']; $cat = array( $_POST['cat'] ); $tags = trim( $_POST['my_tags'] ); $submit = $_POST['submit']; if(isset($submit)){ global $user_ID; $new_post = array( 'post_title' => $postTitle, 'post_content' => $post, 'post_status' => 'publish', 'tags_input' => $tags, 'post_date' => date('Y-m-d H:i:s'), 'post_author' => $user_ID, 'post_type' => 'post', 'post_category' => $cat ); wp_insert_post($new_post,$wp_error); } ?>
BEFORE BACKEND POST :
AFTER BACKEND POST:
Thank You.