Submit WordPress Posts From The Frontend

 How to Allow Users to Submit Posts to WordPress Site

If you’re looking how  to Submit WordPress Posts From The Frontend and  add new posts without having to log into the WordPress dashboard, or allow your visitors a way to submit some content of their own, then here’s a way you can create a new post form and display it on a custom page template.

In order to write a new post in WordPress you have to do a number of steps just to get started. First you must login to get to the dashboard, then go to posts, and then add new post.

 

If you have members of your site that join so they can submit some kind of content or another, it can be even better for them as well to not have to go through all the steps it takes in submitting a new post. Plus this can be used for making a new post form for custom post types, too.

 

1. The Post Form

First we will set up the basic form which you can use to enter in the information to submit new posts.

<!– New Post Form –>
<?php $post_type = “product”;  // here  you can define your post type like post and other post type name ?>
<div id=”custom_form”>
<form id=”create_post” name=”create_post” method=”post” action=””>
<p><label for=”title”>Post Title</label><br />
<input type=”text” id=”title” value=”” tabindex=”1″ size=”20″ name=”title” />
</p>
<p><label for=”description”>Post Description</label><br />
<textarea id=”description” tabindex=”3″ name=”description” cols=”20″ rows=”6″></textarea>
</p>
<p><?php wp_dropdown_categories( ‘show_option_none=Category&tab_index=4&taxonomy=category’ ); ?></p>
<p><label for=”post_tags”>Tags</label>
<input type=”text” value=”” tabindex=”5″ size=”16″ name=”post_tags” id=”post_tags” /></p>
<p align=”right”><input type=”submit” value=”Publish” tabindex=”6″ id=”submit” name=”submit” /></p>
<input type=”hidden” name=”post_type” id=”post_type” value=”<?php echo $post_type; ?>” />
<input type=”hidden” name=”action” value=”post” />
<?php wp_nonce_field( ‘new-post’ ); ?>
</form>
</div>


<!–// New Post Form –>

There’s nothing special with this form really. The only thing is the wp_dropdown_categories() function, which creates the list of categories you can select from.  Everything else you enter into the form will automatically create a new entry, including tags. Make sure no matter what you do, always include the wp_nonce_field(), for security reasons.

2. The PHP for Processing the Form

This is the PHP which you need to process and submit the form information.

Set the post_status as ‘pending’ if you want the post to be approved by the admin, before being published. Else, set it to ‘published’.

<?php
if( ‘POST’ == $_SERVER[‘REQUEST_METHOD’] && !empty( $_POST[‘action’] )) {

// Do some minor form validation to make sure there is content
if (isset ($_POST[‘title’])) {
$title =  $_POST[‘title’];
} else {
echo ‘Please enter a title’;
}
if (isset ($_POST[‘description’])) {
$description = $_POST[‘description’];
} else {
echo ‘Please enter the content’;
}
$tags = $_POST[‘post_tags’];


// Add the content of the form to $post as an array
$post = array(
‘post_title’ => $title,
‘post_content’ => $description,
‘post_category’ => $_POST[‘cat’], // Usable for custom taxonomies too
‘tags_input’ => $tags,
‘post_status’ => ‘publish’, // Choose: publish,draft, preview, future, etc. According to your need if you want post save with draft status then put draft
‘post_type’ => $_POST[‘post_type’] // Use a custom post type if you want to
);
wp_insert_post($post); // Pass  the value of $post to WordPress the insert function
wp_redirect( home_url() );
} // end IF

// Do the wp_insert_post action to insert it
do_action(‘wp_insert_post’, ‘wp_insert_post’);

?>

 

Let’s go over this.

The first line checks if a post has been submitted, and if the action is not empty. You could do this check a number of ways, this is just how I’ve done it:

if( ‘POST’ == $_SERVER[‘REQUEST_METHOD’] && !empty( $_POST[‘action’] )) {

The second part is a basic form validation that checks to see if the title and description fields are set, and if so set the values to the variables $title and $description. You may want to check that each of the fields are set this way but I am just showing as an example.

Then, I set up a variable called $post, which is an array of values from the form fields.

  • post_title = $title like we set above using the validation
  • post_content = $description as we set
  • post_category = the category that was selected
  • tags_input = the tags
  • post_status = publish, future or draft. Whatever you want to set it to
  • post_type = post, page, or if you have set up a custom post type you can use that instead

 

To insert the new post information you can use the function wp_insert_post() and passing the $post variable to it. This is all of course, only if the form has been submitted which is why we put all this within the ‘if’ statement.

Update: I’ve added the function wp_redirect() after the wp_insert_post() function, to fix the redirect to a 404 page. You can change the location by passing the function an argument. For example: wp_redirect( ‘/some-page/’) or wp_redirect( home_url() ).

Close the IF statement and then add the action. The line do_action(‘wp_insert_post’, ‘wp_insert_post’); will add the wp_insert_post hook, with the wp_insert_post from the if statement as the callback, thereby adding the $post variable with all it’s information to the function. Without this line the post will not be submitted and your form will be useless.

 

You can embed this code in a custom page template, say for example: page-submit.php. Copy the code from page.php and paste it in your custom template. You can create custom pate templates using page-slug.php or page-id.php. I find making custom templates this way 100 times better than using the old way, which involves assigning the page a specific template in the page attributes.

 

Make sure that you create a new page in the WordPress dashboard, and that your page slug or page id match the name of your template file (ie: page-submit.php – page slug = submit). This new page you create will automatically use the custom template you make, so there is no need to do anything else. You can enter content into the page if you choose to of course, but as long as the custom template includes the new post form, either above or below the_content() function you are good to go.

 

If you have any questions about this, or can’t get it to work let me know in the comments.

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

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

29 − 23 =

%d bloggers like this: