Welcome to WebmasterWorld Guest from

Forum Moderators: lorax & rogerd

User 'front end' posts code

user posts

7:30 pm on Apr 10, 2013 (gmt 0)

5+ Year Member

Hi all,

I have a tutorial I followed to let users submit posts through the front end and it works like a charm. My only problem is that all of my sites posts are output in a table at the bottom of my page.

I would like to modify the code so that only what a user types into the input fields is the only output onto that specific page and no other posts are populated on that page. Basically whatever they enter is posted on that page and nothing else.

Any way to do this?

My code:

<?php /* Template Name: Insert Posts */

$postTitleError = '';

if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {

if(trim($_POST['postTitle']) === '') {
$postTitleError = 'Please enter a title.';
$hasError = true;
} else {
$postTitle = trim($_POST['postTitle']);

$post_information = array(
'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
'post_content' => esc_attr(strip_tags($_POST['postContent'])),
'post-type' => 'post',
'post_status' => 'pending'

$post_id = wp_insert_post($post_information);


// Update Custom Meta
update_post_meta($post_id, 'vsip_custom_one', esc_attr(strip_tags($_POST['customMetaOne'])));
update_post_meta($post_id, 'vsip_custom_two', esc_attr(strip_tags($_POST['customMetaTwo'])));

// Redirect
wp_redirect( home_url() ); exit;

} ?>

<?php include ('header_state.php'); ?>

<!-- #primary BEGIN -->
<div id="primary">

<form action="" id="primaryPostForm" method="POST">


<label for="postTitle"><?php _e('Post\'s Title:', 'framework') ?></label>

<input type="text" name="postTitle" id="postTitle" value="<?php if(isset($_POST['postTitle'])) echo $_POST['postTitle'];?>" class="required" />


<?php if($postTitleError != '') { ?>
<span class="error"><?php echo $postTitleError; ?></span>
<div class="clearfix"></div>
<?php } ?>


<label for="postContent"><?php _e('Post\'s Content:', 'framework') ?></label>

<textarea name="postContent" id="postContent" rows="8" cols="30"><?php if(isset($_POST['postContent'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['postContent']); } else { echo $_POST['postContent']; } } ?></textarea>



<label for="customMetaOne"><?php _e('Custom Meta One:', 'framework') ?></label>

<input type="text" name="customMetaOne" id="customMetaOne" value="<?php if(isset($_POST['customMetaOne'])) echo $_POST['customMetaOne'];?>" />



<label for="customMetaTwo"><?php _e('Custom Meta Two:', 'framework') ?></label>

<input type="text" name="customMetaTwo" id="customMetaTwo" value="<?php if(isset($_POST['customMetaTwo'])) echo $_POST['customMetaTwo'];?>" />



<?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>

<input type="hidden" name="submitted" id="submitted" value="true" />
<button type="submit"><?php _e('Add Post', 'framework') ?></button>



</div><!-- #primary END -->

<div id="the-info">

<!-- #primary BEGIN -->
<div id="primary">

<?php if(isset($_GET['result'])) : ?>

<?php if($_GET['result'] == 'success') : ?>

<!-- .client_success BEGIN -->
<div class="client_success">

<span class="success">Successfully Added<span class="cross"><a href="#">X</a></span></span>

</div><!-- .client_success END -->

<?php endif; ?>

<?php endif; ?>


<th>Post Title</th>
<th>Post Excerpt</th>
<th>Post Status</th>

<?php $query = new WP_Query(array('post_type' => 'post', 'posts_per_page' =>'-1', 'post_status' => array('publish', 'pending', 'draft', 'private', 'trash') ) ); ?>

<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>

<td><?php echo get_the_title(); ?></td>
<td><?php the_excerpt(); ?></td>
<td><?php echo get_post_status( get_the_ID() ) ?></td>

<?php $edit_post = add_query_arg('post', get_the_ID(), get_permalink(61 + $_POST['_wp_http_referer'])); ?>

<a href="<?php echo $edit_post; ?>">Edit</a>

<?php if( !(get_post_status() == 'trash') ) : ?>

<a onclick="return confirm('Are you sure you wish to delete post: <?php echo get_the_title() ?>?')"href="<?php echo get_delete_post_link( get_the_ID() ); ?>">Delete</a>

<?php endif; ?>

<?php endwhile; endif; ?>


</div><!-- #primary END -->


<?php get_footer(); ?>

Featured Threads

Hot Threads This Week

Hot Threads This Month