Simple and Easy to Submit Your Post


Sometimes we need to create a WordPress more easy and simple, make adding new posts without having to log into the WordPress dashboard, or maybe to allow the 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.

In ordinary way, that’s a total of 3 page refreshes. For the first step, you need go to your wp-admin. Second step, you need to type your name and password as well. Last, you need add new post in your dashboard and publish your post. Yeah that’s long step to publish some post in your blog.

With this way you just enter the form and click “publish” button in your frontend, but then you still have to go to edit the post if you want to add a category or some tags plus any other things you may use in your post.

Okay, i will give you the tutorial step by step to making submit post from the frontend.

First we need create the form

<!-- New Post Form --><div id="postbox">
<form id="new_post" name="new_post" method="post" action="">
<p><label for="title">Title</label><br /> <input type="text" id="title" value="" tabindex="1" size="20" name="title" /> </p>
<p><label for="description">Description</label><br /> <textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea> </p>
<p><label for="async-upload">Image</label><br />
    <input type="file" id="async-upload" value="" tabindex="1" size="20" name="async-upload" />
</p>
<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></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="post" />
<input type="hidden" name="action" value="post" /> <?php wp_nonce_field( 'new-post' ); ?>
</form> </div> <!--// New Post Form -->

Here is where the processing begins. This is the PHP which you need to submit the form information

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';
}

// Add the content of the form to $post as an array
$post = array(
'post_title'    => $title,
'post_content'    => $description,
'post_category'    =>array( (int)$_POST['cat'] ),  // Usable for custom taxonomies too
'post_status'    => 'publish',            // Choose: publish, preview, future, etc.
'post_type'        => $_POST['post_type']  // Use a custom post type if you want to
);
// Pass  the value of $post to WordPress the insert function
// http://codex.wordpress.org/Function_Reference/wp_insert_post
$newPost = wp_insert_post($post);

//insert image
require_once(ABSPATH . 'wp-admin/includes/admin.php');
$attachmentId = media_handle_upload('async-upload'); //post id of Client Files page
add_post_meta($newPost, 'image', wp_get_attachment_url($attachmentId));

wp_redirect( home_url() ); // redirect to home page after submit
// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post');

} // end IF

And done! :).  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

If you any further questions or have suggestion on what would you like to see on our next How-To post, feel free to leave them in the comment bellow.

Lets be creative!

Advertisements

5 thoughts on “Simple and Easy to Submit Your Post

  1. Anang,

    (1) Does all this code go into the html where the form will be located? I am building a WordPress Multisite Network and want to insert the form into a widget that appears in each site admin’s dashboard.

    (2) The form is to edit an existing post, which means I need the post ID. I know the category and the post slug used in the URL, but I am thinking the post ID will change from blog site to blog site.

    Thanks,
    Kirk

    1. Hi Kirkward, to sue that code you need to create new page template of your themes, and put all of that code into the file. And this function doesn’t need post ID cause we just insert the post into database and ID will be auto generate by wp_insert_post()

    1. Hi Ansar,

      Sorry for the late response. Right now i can’t make some video tutorial for you, but you can try all of instruction from this tutorial, if you have got some error or stuck in some code, you can ask me and i really happy to make your code work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s