How to Add a Delete Post Link to Your WordPress Posts


Some sites, especially those that allow contributors to post, may need an easy way to delete posts from time to time.

In order to make it as easy as possible, of course, the best thing to do would be to put a “Delete” button right on the post itself. Of course this delete link should only be viewable by logged in users with a certain permission level, much like an “Edit” button that many themes use.

Here’s how you can go about doing that.

First, place the following code in you functions file after the opening <php tag. (Appearance > Editor > Theme Functions – functions.php)

function wp_delete_post_link($link = 'Delete This', $before = '', $after = '')
{
global $post;
if ( $post->post_type == 'page' ) {
if ( !current_user_can( 'edit_page', $post->ID ) )
return;
} else {
if ( !current_user_can( 'edit_post', $post->ID ) )
return;
}
$link = "<a href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&amp;post=" . $post->ID, 'delete-post_' . $post->ID) . "'>".$link."</a>";
echo $before . $link . $after;
}

 

Next, you will need to put some code in your theme files where you would like your delete link to show up.

I put the following code in my single.php code just above where the content of the post starts. (Appearance > Editor > Single Post – single.php)

<?php wp_delete_post_link('Delete This', '<p>', '</p>'); ?>

 

The '<p>', '</p>' in the code gives it space above it and below, but if you don’t want that space, then you can take it out:

 

<?php wp_delete_post_link('Delete This'); ?>

 

You could also put this on your Main Index Template, for example, if you wanted the link to show up on the home page too. (Appearance > Editor > Main Index Template – index.php)

Featured Plugin - WordPress Q&A Site Plugin

It's now incredibly easy to start your own Q&A site using nothing more than WordPress - The Q&A plugin simply and brilliantly transforms any site, or page, into a perfect support or Q&A environment.
Find out more

And here’s what it looks like.

 

Thanks to web-templates.nu for the code.

Featured Plugin - WordPress Appointments Plugin

Take, set and manage appointments and client bookings without having to leave WordPress. Appointments+ makes it easy.
Find out more

Photo: Delete sticker icon. Vector illustration. From BigStock

Comments (4)

  1. Anyway to add a publish post button at frond-end. Actually I’ve drafts page for editors and I want to add a publish for each post, so that they will be able moderate posts written by authors, without going to admin panel.

  2. Thanks a lot for the code. I run an autoblog with rss feeds and I get lots of empty or garbage posts. Your code does exactly what I need and now I can delete those posts directly from the homepage easily.

    Could you tell me how to stop redirecting to the homepage after I delete a post? It will be a huge time saver for me if I can use the the middle mouse button (ctrl+click) to click the delete link so it would open in a new window, but without loading the hompage. (maybe a blank page or auto close the page after I delete a post.)

    Thanks again.

Participate