Allow WordPress Authors to Delete their Posts from the Front End

Okay so let’s say you have a WordPress installation customized so that users don’t really need to see the backend at all. Perhaps you’ve used TDO Mini-forms (which is no longer supported, so I don’t recommend it) or Gravity Forms (which is truly awesome) to allow users to submit posts via your site’s front end. Perhaps you’ve even added your own code, or a plugin, that allows your users to edit their profiles from the front end.

But gosh darnit, what if you want to allow users to delete their posts from the front end? There are some plugins out there that allow you to edit an entire post from the front end, but none that I could find which allow you to delete the post entirely (actually, we’re moving the post to the Trash, but small difference).

How to Allow WP Authors to Delete their own Posts without Accessing the Backend

The code:

<?php if ($post->post_author == $current_user->ID) { ?>
   <p><a onclick="return confirm('Are you SURE you want to delete this post?')" href="<?php echo get_delete_post_link( $post->ID ) ?>">Delete post</a></p>
<?php } ?>

Let’s break it apart, shall we?

if ($post->post_author == $current_user->ID) {

Here we check to see if the current user is logged in and if they’re the author of the post. If so, we continue, if not, we don’t do anything and they don’t see the link to delete the post.

onclick="return confirm('Are you SURE you want to delete this post?')"

Here we simply use some Javascript to popup a warning window asking them to confirm they want to delete the post. There is no undo with this method.

echo get_delete_post_link( $post->ID )

This is a built in WordPress function which will do all of the heavy lifting for us.

Tada! That’s it!

Up Next: How to Restrict WordPress Front End "Edit" Links to Admins Only