If you’re looking to add new posts from front end without using any plugin into the WordPress and allow your visitors a way to submit some kind of content of their own, then here’s a way you can create a new post form and display it on a custom page template.
Today, we are going to explain how to insert or add posts from the front end. Inserting post from front end in WordPress is important functionality when we looking guest post, this is very simple and common process you will be enjoy.
WordPress allow to users add post functionality using WordPress dashboard, using that Admin, Author and contribute can be submit post if you looking logged in used don’t use dashboard and add post from front end then you just follow some simple steps using that you will achieve you goal.
Step 1. Create Template Page
First of you should create template page in your WordPress theme that allow you to show custom post functionality on front end.
<?php /* ** Template Name: Add Post From Frontend */ ?>
Step 2. Creating a Form
First of you create HTML from which will be allow user to insert post detail such as post title, post content, select category and choose featured image if you want.
<div class="col-sm-12"> <h3>Add New Post</h3> <form class="form-horizontal" name="form" method="post" enctype="multipart/form-data"> <input type="hidden" name="ispost" value="1" /> <input type="hidden" name="userid" value="" /> <div class="col-md-12"> <label class="control-label">Title</label> <input type="text" class="form-control" name="title" /> </div> <div class="col-md-12"> <label class="control-label">Sample Content</label> <textarea class="form-control" rows="8" name="sample_content"></textarea> </div> <div class="col-md-12"> <label class="control-label">Choose Category</label> <select name="category" class="form-control"> <?php $catList = get_categories(); foreach($catList as $listval) { echo '<option value="'.$listval->term_id.'">'.$listval->name.'</option>'; } ?> </select> </div> <div class="col-md-12"> <label class="control-label">Upload Post Image</label> <input type="file" name="sample_image" class="form-control" /> </div> <div class="col-md-12"> <input type="submit" class="btn btn-primary" value="SUBMIT" name="submitpost" /> </div> </form> <div class="clearfix"></div> </div>
Step 3. Add Post Form Validation
Here we using simple JavaScript forms validations, according your requirement you can implement PHP validation also.
<script> function returnformValidations() { var title = document.getElementById("title").value; var content = document.getElementById("content").value; var category = document.getElementById("category").value; if(title=="") { alert("Please enter post title!"); return false; } if(content=="") { alert("Please enter post content!"); return false; } if(category=="") { alert("Please choose post category!"); return false; } } </script>
Step 4. PHP Code for add post from font end
Here we are writing PHP code for insert post in WordPress. While you open page first it will be check user is logged in or not if user not login user cant add post message will be show.
<?php if(is_user_logged_in()) { if(isset($_POST['ispost'])) { global $current_user; get_currentuserinfo(); $user_login = $current_user->user_login; $user_email = $current_user->user_email; $user_firstname = $current_user->user_firstname; $user_lastname = $current_user->user_lastname; $user_id = $current_user->ID; $post_title = $_POST['title']; $sample_image = $_FILES['sample_image']['name']; $post_content = $_POST['sample_content']; $category = $_POST['category']; $new_post = array( 'post_title' => $post_title, 'post_content' => $post_content, 'post_status' => 'pending', 'post_name' => 'pending', 'post_type' => $post_type, 'post_category' => $category ); $pid = wp_insert_post($new_post); add_post_meta($pid, 'meta_key', true); if (!function_exists('wp_generate_attachment_metadata')) { require_once(ABSPATH . "wp-admin" . '/includes/image.php'); require_once(ABSPATH . "wp-admin" . '/includes/file.php'); require_once(ABSPATH . "wp-admin" . '/includes/media.php'); } if ($_FILES) { foreach ($_FILES as $file => $array) { if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) { return "upload error : " . $_FILES[$file]['error']; } $attach_id = media_handle_upload( $file, $pid ); } } if ($attach_id > 0) { //and if you want to set that image as Post then use: update_post_meta($pid, '_thumbnail_id', $attach_id); } $my_post1 = get_post($attach_id); $my_post2 = get_post($pid); $my_post = array_merge($my_post1, $my_post2); } } else { echo "<h2 style='text-align:center;'>User must be login for add post!</h2>"; } ?>
Method 2
You need to follow the below essential steps for making a form for user submitted posts
Step 1. Create a form by which a user can submit the post title, content, tags, category, featured image. You can put this code into your theme functions.php or any other file.
<?php add_shortcode( 'themedomain_frontend_post', 'themedomain_frontend_post' ); function themedomain_frontend_post() { themedomain_post_if_submitted(); ?> <form id="new_post" name="new_post" method="post" enctype="multipart/form-data"> <p><label for="title"><?php echo esc_html__('Title','theme-domain'); ?></label><br /> <input type="text" id="title" value="" tabindex="1" size="20" name="title" /> </p> <?php wp_editor( '', 'content' ); ?> <p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p> <p><label for="post_tags"><?php echo esc_html__('Tags','theme-domain'); ?></label> <input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p> <input type="file" name="post_image" id="post_image" aria-required="true"> <p><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p> </form> <?php } ?>
Step 2. Capturing the input from the front-end user post submission form. You can also put this code into your theme functions.php or any other file.
<?php function themedomain_post_if_submitted() { // Stop running function if form wasn't submitted if ( !isset($_POST['title']) ) { return; } // Add the content of the form to $post as an array $post = array( 'post_title' => $_POST['title'], 'post_content' => $_POST['content'], 'post_category' => array($_POST['cat']), 'tags_input' => $_POST['post_tags'], 'post_status' => 'draft', // Could be: publish 'post_type' => 'post' // Could be: 'page' or your CPT ); $post_id = wp_insert_post($post); // For Featured Image if( !function_exists('wp_generate_attachment_metadata')){ require_once(ABSPATH . "wp-admin" . '/includes/image.php'); require_once(ABSPATH . "wp-admin" . '/includes/file.php'); require_once(ABSPATH . "wp-admin" . '/includes/media.php'); } if($_FILES) { foreach( $_FILES as $file => $array ) { if($_FILES[$file]['error'] !== UPLOAD_ERR_OK){ return "upload error : " . $_FILES[$file]['error']; } $attach_id = media_handle_upload( $file, $post_id ); } } if($attach_id > 0) { update_post_meta( $post_id,'_thumbnail_id', $attach_id ); } echo 'Saved your post successfully! :)'; } ?>
Step 3. Paste the form shortcode where you want to display the form
<?php echo do_shortcode('[themedomain_frontend_post]'); ?>
That’s it.
I think the post will be helpful for you and save your time.
Happy Coding. Thanks to phpkida.com and techsolutionshere.com