How to add post from frontend in WordPress without plugin

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 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($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>";
}
?>
Mukesh Jakhar - PHP Web Developer
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 to problems which i already solved.

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.