WordPress: Adding Posts From The Front End

As a new addition to the blog, I am going to start offering some WordPress tutorials and various how-tos about things that I have found through hours on google and toiling endlessly myself.  While the examples you will find are in no way exhaustive or even perhaps the best way, they are all ways that I have implemented in previous projects and found useful.

Skills Needed:

  • Intermediate HTML
  • Basic CSS
  • Basic-Intermediate PHP
  • intermediate WordPress theme knowledge

Why post from the front end

There are various reasons to allow clients, users, or customers to post from the front end.  Everything from review sites to community blogging sites.  My personal use involved the creation of a HelpDesk for a client founded on WordPress.  Users needed to be able to login (as to prevent non employees from submitting), but the submission process needed to be quick and painless (I know, WordPress is just SO hard).  So I spent some hours on google and in the code and finally came up with a method that pleased both my own standards and that of the client.

Creating the Page Template

Assuming that you have a working knowledge of WordPress page templates (if not, check out this resource), go ahead and create a page template like the one below.


Next we enter the code necessary to submit the post itself.  This code wraps the rest of our code and tells it to submit to the database as a post.

<?php
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

Next, we tell WordPress to do some minor form validation.

if ($_POST['title'] != '') {
		$title =  $_POST['title'];
	} else {
		echo 'Please enter the wine name';
	}
	if ($_POST['description'] != '') {
		$description = $_POST['description'];
	} else {
		echo 'Please enter some notes';
	}

After we validate some of our post information (to make sure that we have enough info to write a post), we proceed to storing the form inputs to our array ‘$new_post.’

 // ADD THE FORM INPUT TO $new_post ARRAY
    $new_post = array(
    'ticket_number' => $ticket_number,
    'room_number' => $room_number,
    'post_content'  =>   $description,
    'post_category' => array($_POST['cat']),
    'tags_input'    =>   array($tags),
    'post_status'   =>   'publish',           // Choose: publish, preview, future, draft, etc.
    'post_type' =>   'post',  //'post',page' or use a custom post type if you want to
     $emergency = $_POST['emergency'],
     $location = $_POST['location'],
     $problem_type = $_POST['problem_type'],
    );

	 //SAVE THE POST
    $pid = wp_insert_post($new_post);

	$update_post = array();   // create empty array
	$update_post['ID'] = $pid;   // set post ID to be updated
	$update_post['post_title'] = $pid;  // set new value for title in that post
	wp_update_post( $update_post );  // update the post

	add_post_meta($pid, 'emergency', $emergency, true);
	add_post_meta($pid, 'location', $location, true);
	add_post_meta($pid, 'problem_type', $problem_type, false);
	add_post_meta($pid, 'ticket_number', $ticket_number, false);
	add_post_meta($pid, 'room_number', $room_number, false);
    //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
    wp_set_post_tags($pid, $_POST['post_tags']);

    //REDIRECT TO THE NEW POST ON SAVE
    $link = get_permalink( $pid );
    wp_redirect( $link );

    } // END THE IF STATEMENT THAT STARTED THE FORM

    //Publish the post
    do_action('wp_insert_post', 'wp_insert_post');

?>

After we complete the necessary code to publish the post, we move on to the fun stuff, the meat of our form for posting from the front end! The form below is my form, but yours will look different. Feel free to copy and paste anything you need, or ask questions in the comments!

<div class="content_loop_ticket">
                    	<div class="content_loop_ticket_status">
                            <div class="content_loop_ticket_status_<?php $category = get_the_category(); echo $category[0]->cat_name; ?>">
                                <h3><?php $category = get_the_category(); echo $category[0]->cat_name; ?></h3>
                            </div>
                        </div>
                        <div class="content_loop_ticket_info">
                           <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?> | <?php the_author(); ?> | <?php the_time('m/d/Y'); ?></h2></a>
                        </div>
						<div class="content_loop_ticket_emergency_<?php echo get_post_meta($post->ID, "emergency", true); ?>">
                        	<h3>EMERGENCY</h3>
                        </div>
                    </div>

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>