Edit and Delete posts from front end with no plugin

This is a continuous tutorial to the “post from your front end” tutorial by voodoo. I will break the tutorial to two parts to make it easier. First part will be creating “my-posts.php” template file. This file displays a list of posts (approved and pending) of a user when he is logged in when each post will have “Edit” and “Delete” buttons. Clicking on “delete” button of a post will instantly delete it. Clicking on “Edit” will send the user to “edit-post.php” file that we will create in the second part of this tutorial.

Below is the code of the “my-posts.php”. i use the one from my project as example. i created it for a coupons site so you will have to change few things to make it work in you site. i will show the code by parts from top to bottom while trying to explain it the best way.

In order for a user to see his posts he must be logged in to the site. In my site there is “my coupons” button that is being displayed only when a user is logged in and only when clicking on this button the user can get to this page. However, just to be a little more safe i put the code below at the top of the page that check if a user is logged in or not. change the url inside the code to where ever you want the user to be redirect when he is not logged in (usually to login page).


<?php if ( !is_user_logged_in()) {
	wp_redirect( 'www.yoursite.com/login/' );  /* this is where user will be redirected if he is not logged in. change to desired url */
	} else {
?>
<?php
/**
 * Template file displaying My coupons page.
 * Created by Eyal Fitoussi
 *
 */

The next code will run every time a user click on delete button. It will set a query variable “postid1″ with the post ID that stored in “postid”. the next line of the code will delete the post.


if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
          set_query_var( 'postid1', $_POST['postid'] );     /*  set the "postid" value from the delete button of the post we choose to delete into "postid1" */

	 wp_delete_post( get_query_var( 'postid1'), true );  /* delete the post we choosed   */
 };

Next we have the query_posts. It will display only the published posts of the logged in user. you can see that the ‘author’ is set to $current_user->ID and ‘post_status’ set to ‘publish’. the only things we need to change there is the post_type. mine is set to “coupons”. change it to your post type name.


get_header(); ?>
      <div id="content" role="main">
        <h1 class="entry-title"><?php the_title(); ?></h1>

	<?php /* query_posts of approved post of the logged in user */ ?>
        <?php query_posts( array( 'author' => $current_user->ID,'post_status' => 'publish' , 'post_type' => array( 'coupons' )  ) ); ?> <?php /* change 'post_type" value insted or coupon to your post type name. */ ?>

next is the message that will be displayed if no post were found. you can change the “no coupons found” to whatever you want.


<?php if ( !have_posts() ): ?> <?php /* if no approved posts exist, write a message */ ?>
       <br />
        <div class="info" id="message">
             <p><?php _e( 'No coupons found.', 'coupons' ); ?></p> <?php /* change the message "no coupons found to your message */ ?>
        </div>
<?php endif; ?>

Now we will Display our posts. Each post in the loop will have the title which will be a link to the post, the post thumbnail, the content of the post and the Delete and Edit buttons. in the buttons you can see how “postid” get the ID value of the post and store it (lines 14 and 18) so later it will be used to delete or edit the post we want. the only thing we will change in this part of the code is the url of the edit-post button. we need to change it to where we will later save our “edit-post.php” template file. go to line 13 and change the url:


<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> <?php /* start the posts loop */ ?>
     <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
         <div class="entry-content">
		<div class="coupon-wrapper">
		     <div class="coupon-header">
			<div class="coupon-title"><a href="<?php the_permalink(); ?>"><?php echo $post->post_title; ?></a></div> <?php /* display the post title */ ?>
		     </div> <?php /* coupon header */ ?>
		     <div class="coupon-info">
	                  <div class="coupon-image"><?php echo get_the_post_thumbnail( get_the_ID(), array( 200, 150 ) ); ?></div> <?php /* displays the post thumbnail */ ?>
			  <div class="coupon-description"><?php the_content(); ?></div> <?php /* displays the post content */ ?>
		     </div> <?php /* coupon info */ ?>
		     <div class="coupon-buts">
	                  <form class="edit-coupon" action="http://yoursite.com/edit-post.php" method="post"> <?php /* the url of the "edit-post.php" we will create later */ ?>
				<input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <?php /* get the post ID into "postid" and later pass it to "edit-post.php" */ ?>
            			<input type="submit"  value="Edit" />
       			  </form>
       			  <form class="delete-coupon" action="" method="post">
				<input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <?php /* get the post ID into "postid" and later delete the post */ ?>
            			<input type="submit"  value="Delete" />
       			  </form>
       		    </div> <?php /* coupon buts */ ?>
	       </div> <?php /* coupon wrapper */ ?>
        </div> <?php /* entry-content */ ?>
   </div> <?php /* #post-## */ ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<div class="clear"></div>

The next code is almost the same as the codes above. the only different is that it creates a pending query posts instead of published. go over the code and changed the same things we changed before.
- line 3 change post_type ‘coupons’ to your post type.
- line 8 change the message “no pending coupons found”.
- line 25 change the url to where the “edit-post.php” template file will be


 <h3>Pending Approval</h3>
                <?php /* query_posts for the approved post of the logged in user */ ?>
                <?php query_posts( array( 'author' => $current_user->ID,'post_status' => 'pending' , 'post_type' => array( 'coupons' )  ) ); ?> <?php /* change 'post_type" value insted or coupon to your post type name. */ ?>

                <?php if ( !have_posts() ): ?>
                    <br />
                    <div class="info" id="message">
                        <p><?php _e( 'No Pending coupons found.', 'coupons' ); ?></p> <?php /* change the message "no pending coupons found to your message */ ?>
                    </div>
                <?php endif; ?>

                <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> <?php /*start the pending posts loop */ ?>

                    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                        <div class="entry-content">
							<div class="coupon-wrapper">
								<div class="coupon-header">
									<div class="coupon-title"><a href="<?php the_permalink(); ?>"><?php echo $post->post_title; ?></a></div> <?php /* display the post title */ ?>
								</div> <?php /* coupon header */ ?>
								<div class="coupon-info">
									<div class="coupon-image"><?php echo get_the_post_thumbnail( get_the_ID(), array( 200, 150 ) ); ?></div> <?php /* displays the post thumbnail */ ?>
									<div class="coupon-description"><?php the_content(); ?></div> <?php /* displays the post content */ ?>
								</div> <?php /* coupon info */ ?>
						        <div class="coupon-buts">
						        	<form class="edit-coupon" action="http://yoursite.com/edit-post.php" method="post"> <?php /* the url of the "edit-post.php" we will create later */ ?>
										<input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <?php /* get the post ID into "postid" and later pass it to "edit-post.php" */ ?>
            							<input type="submit"  value="Edit" />
       						 		</form>
       						 		<form class="delete-coupon" action="" method="post">
										<input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <?php /* get the post ID into "postid" and later delete the post */ ?>
            							<input type="submit"  value="Delete" />
       						 		</form>
       						 	</div><?php /* coupon buts */ ?>
							</div><?php /* coupon wrapper */ ?>
                        </div><?php /* .entry-content */ ?>
                    </div><?php /* #post-## */ ?>
                <?php endwhile; ?>
                <?php wp_reset_query(); ?>

the last code is the end of this template file.


</div><?php /* #content */ ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php } ?> <?php /* user is logged in */ ?>

below is the entire code of this tutorial. you can copy and paste it into a code editor (i am using notepad ++) to make it better to read and understand. don’t forget to change the ID and classes tags to make it work with your site.


<?php if ( !is_user_logged_in()) {
	wp_redirect( 'www.yoursite.com/login/' );  /* this is where user will be redirected if he is not logged in. change to desired url */
	} else {
?>
<?php
/**
 * Template file displaying My coupons page.
 * Created by Eyal Fitoussi
 *
 */
if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
	 set_query_var( 'postid1', $_POST['postid'] );     /*  set the "postid" value from the delete button of the post we choose to delete into "postid1" */

	 wp_delete_post( get_query_var( 'postid1'), true );  /* delete the post we choosed   */
	};

get_header(); ?>
            <div id="content" role="main">
                <h1 class="entry-title"><?php the_title(); ?></h1>

				<?php /* query_posts of approved post of the logged in user */ ?>
                <?php query_posts( array( 'author' => $current_user->ID,'post_status' => 'publish' , 'post_type' => array( 'coupons' )  ) ); ?> <?php /* change 'post_type" value insted or coupon to your post type name. */ ?>

                <?php if ( !have_posts() ): ?> <?php /* if no approved posts exist, write a message */ ?>
                    <br />
                    <div class="info" id="message">
                        <p><?php _e( 'No coupons found.', 'coupons' ); ?></p> <?php /* change the message "no coupons found to your message */ ?>
                    </div>
                <?php endif; ?>

                <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> <?php /* start the posts loop */ ?>

                    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                        <div class="entry-content">
							<div class="coupon-wrapper">
								<div class="coupon-header">
									<div class="coupon-title"><a href="<?php the_permalink(); ?>"><?php echo $post->post_title; ?></a></div> <?php /* display the post title */ ?>
								</div> <?php /* coupon header */ ?>
								<div class="coupon-info">
									<div class="coupon-image"><?php echo get_the_post_thumbnail( get_the_ID(), array( 200, 150 ) ); ?></div> <?php /* displays the post thumbnail */ ?>
									<div class="coupon-description"><?php the_content(); ?></div> <?php /* displays the post content */ ?>
								</div> <?php /* coupon info */ ?>
						        <div class="coupon-buts">
						        	<form class="edit-coupon" action="http://yoursite.com/edit-post.php" method="post"> <?php /* the url of the "edit-post.php" we will create later */ ?>
										<input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <?php /* get the post ID into "postid" and later pass it to "edit-post.php" */ ?>
            							<input type="submit"  value="Edit" />
       						 		</form>
       						 		<form class="delete-coupon" action="" method="post">
										<input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <?php /* get the post ID into "postid" and later delete the post */ ?>
            							<input type="submit"  value="Delete" />
       						 		</form>
       						 	</div> <?php /* coupon buts */ ?>
							</div> <?php /* coupon wrapper */ ?>
                        </div> <?php /* entry-content */ ?>
                    </div> <?php /* #post-## */ ?>
                <?php endwhile; ?>
                <?php wp_reset_query(); ?>
                <div class="clear"></div>

                <h3>Pending Approval</h3>
                <?php /* query_posts for the approved post of the logged in user */ ?>
                <?php query_posts( array( 'author' => $current_user->ID,'post_status' => 'pending' , 'post_type' => array( 'coupons' )  ) ); ?> <?php /* change 'post_type" value insted or coupon to your post type name. */ ?>

                <?php if ( !have_posts() ): ?>
                    <br />
                    <div class="info" id="message">
                        <p><?php _e( 'No Pending coupons found.', 'coupons' ); ?></p> <?php /* change the message "no pending coupons found to your message */ ?>
                    </div>
                <?php endif; ?>

                <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> <?php /*start the pending posts loop */ ?>

                    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                        <div class="entry-content">
							<div class="coupon-wrapper">
								<div class="coupon-header">
									<div class="coupon-title"><a href="<?php the_permalink(); ?>"><?php echo $post->post_title; ?></a></div> <?php /* display the post title */ ?>
								</div> <?php /* coupon header */ ?>
								<div class="coupon-info">
									<div class="coupon-image"><?php echo get_the_post_thumbnail( get_the_ID(), array( 200, 150 ) ); ?></div> <?php /* displays the post thumbnail */ ?>
									<div class="coupon-description"><?php the_content(); ?></div> <?php /* displays the post content */ ?>
								</div> <?php /* coupon info */ ?>
						        <div class="coupon-buts">
						        	<form class="edit-coupon" action="http://yoursite.com/edit-post.php" method="post"> <?php /* the url of the "edit-post.php" we will create later */ ?>
										<input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <?php /* get the post ID into "postid" and later pass it to "edit-post.php" */ ?>
            							<input type="submit"  value="Edit" />
       						 		</form>
       						 		<form class="delete-coupon" action="" method="post">
										<input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <?php /* get the post ID into "postid" and later delete the post */ ?>
            							<input type="submit"  value="Delete" />
       						 		</form>
       						 	</div><?php /* coupon buts */ ?>
							</div><?php /* coupon wrapper */ ?>
                        </div><?php /* .entry-content */ ?>
                    </div><?php /* #post-## */ ?>
                <?php endwhile; ?>
                <?php wp_reset_query(); ?>

			</div><?php /* #content */ ?>
			<?php locate_template( array( 'left-sidebar.php' ), true ); ?>

<?php get_footer(); ?>
<?php } ?> <?php /* user is logged in */ ?>

This was a special guest post from Eyal Fitoussi which brings lots of exciting new features to our posting from the front end series. Stay tuned for the next portion, Part 2 of this tutorial is now available, we are going to learn how to edit these posts from the front end! It’s our first guest post, and we’d love to have your comments! – Rev. Voodoo

15 thoughts on “Edit and Delete posts from front end with no plugin”

  1. Very cool tutorial! I can’t wait to see the second part! I have a break from classes coming up in a few weeks, I can’t wait to use this code when I finally have some time to play around!

    1. Thank you. i hope you guys find it helpful and actually make it work with your site. the second part will probably take little longer since there is more into it. but i will try to finish it by this weekend. also, i hope you guys excuse my english grammar or any misspellings. :)

  2. I am sorry guys for the delay. Shortly after my first post I had to go to my country and I came back only a week ago. I am going to start the second post tonight.
    @paul, did you try the first part? Did it work for you?

    1. It’s no problem! Your efforts are much appreciated! I’m really tied down at work, and the holidays and everything. I’m really hoping to start at my attempt to bring the full wp editor to the front end by playing around with the new API very soon.

    2. Hello! Wow, would be great to get the second part… I have a custom solution for users to post custom-posts from the front-end, but I need something for editing. I have got so far as to make an editable form that gets the post content, and then updates it with wp_update_post, but I’m stuck at the part where the form is submitted, the page refreshes, but then the new content only appears after another refresh (manual) of the page :-) So I’ll be patiently waiting for your solution! Thanks so much for taking your time to do this! It’s incredibly appreciated!

    1. It sure does! I’m super grateful that someone has stepped up and offered a solution for all the VoodooPress readers! I can’t wait until I’m done with school, and have some time to have fun again, writing code for WordPress!

  3. I dont know why, but when I click the form submit button, nothing is posted to the url. Could this be because my action links to a page permalink with the edit.php as a template?

    1. I can’t answer questions on this too well, as I’ve never implemented this on any site yet. Hopefully the author will have time to help you out. Have you actually tried changing your action to point directly to the edit template rather than the permalink? I’m not sure if that would make a difference…

      1. ya, it seems to work when I dont use a template, but then I have to reload the wordpress header, and the url is messy.

Leave a Reply