How to add post from frontend in wordpress without plugin

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 requirment 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
);

$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($new_post, '_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>";
}
?>

Author: Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.
I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution of problems which i already solved.

Leave a Reply

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

Related Posts: You may like

How to Add Post Thumbnail to RSS Feed in WordPress

How to track contact form 7 form in salesforce without plugin

Contact Form 7 Redirect to Thank You Page

How to Validation Business Email in Contact Form 7 WordPress

How to Create and get WordPress Custom Field

WordPress Websites Are SEO Friendly Or Not

How to get last insert id in WordPress

How to Change WordPress Email From Sender Name Email Settings Without Plugin

How to Create Custom Shortcodes in WordPress

how to fetch custom post by tags in WordPress

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.