I recently had a client ask for a delete post link, like the WordPress edit post link template tag, for the user to delete posts when browsing the front-end of the site. He also wanted a pop-up confirmation box to appear before deleting the post. I saw variations on this function in several places, but never exactly what he asked for. So here it is:
- a link to delete (or “trash”) a post from the front-end
- shows only if the user is logged in and has the capability to delete that post
- shows a confirmation pop-up box
- if confirmed, post is trashed and page is refreshed
The following goes in the theme’s functions.php file:
function wp_delete_post_link($link = 'Trash This', $before = '', $after = '') {
global $post;
$post_type_object = get_post_type_object( $post->post_type );
if ( !current_user_can( $post_type_object->cap->delete_post, $post->ID ) )
return;
$message = "Are you sure you want to trash ".get_the_title($post->ID)." ?";
$delLink = wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID);
$htmllink = "<a href='" . $delLink . "' onclick = \"if ( confirm('".$message."' ) ) { execute(); return true; } return false;\"/>".$link."</a>";
echo $before . $htmllink . $after;
}
Then use the following template tag wherever you want it to appear in your theme.
<?php wp_delete_post_link('(trash)', '<div class="post-edit">', '</div>'); ?>
Some were asking about a url redirect for after the post is trashed. Try this to redirect to the home page:
add_action('trashed_post','my_trashed_post_handler',10,1);
function my_trashed_post_handler($post_id) {
if (!is_admin()) {
global $post;
$user_info = get_userdata($post->post_author);
wp_redirect(get_option('siteurl'));
exit;
}
}

Nice, thanks!
But it outputs only a text, no link at all?
You’re saying the link doesn’t come through, just the text, right? It should come through with a link and whatever text you put into the template tag.
Hi I am getting the same problem. It just outputs the text ‘Trash’ but it is not clickable.
Any ideas? great idea!
Sorry guys, WP was interpreting my link code as an actual link. I think I have it input right, so you can see the “a href” part in there after $link. Let me know…
Hi, the link work for me!
I ask you: there is a way to redirect to home page (or another page) after the post deleting?
Thank you for the code and for the replies.
Thanks! This worked perfectly for my client. He wants to manage the site without having to go into the backend :/
Hi!
I tried this code, but can’t get it to work.
I’m using it in a custom column for a custom post type, could it be because of this?
When I press trash I get the popup asking for confirmation, and when I proceed I get this error message:
Are you sure you want to do this?
Please try again.
The only option is to press “Please try again.” which takes me back to the view that lists all posts in the admin area.
Per–are you using a Mac? I’m on Windows, and the popup box says, “Are you sure you want to delete ‘Post Title’?” and then two buttons below that say “Ok” and “Cancel”. But this might work differently on different operating systems.
Yes, I’m using a Mac.
That popup shows up, it’s when I press “Ok” that I get the error message.
Tried it on Windows 7 now with same results..
It does not work for me, either.
It just output a message “Are you sure you want to do this? Please try again”.
Using the latest stable wordpress version.
Just modded the code a bit–maybe it will work for the last commenters now? There are a lot of quotation and single quotes in there than can get messed up easily. It is still working okay for me though. If any of you find out what problem you were having, please report back.
Hi,
I’m getting that “Are you sure you want to do this? Please try again” too … “Please try again” takes me back to previous page (the page from where I tried to delete)…
I believe there are no errors in quotations, code etc.
Any thoughts ?
As above
“Are you sure you want to do this? Please try again” too … “Please try again” takes me back to previous page (the page from where I tried to delete)
Any update. as it appears you’re nearly there.
Hi,
How can I edit this so it also allows non registered users to delete posts?
Thanks
wow.. thanks so much for posting this up!! I thought I was gonna have to put out a few hours to figure how to code this. Thanks again!
It works gr8 for me , but in a single post(inner page0 redirection after deleting is not proper,
Any idea will be highly appriciable.
Thanks
Peer
Hey I resolved the above redirection problem;
just add
to
in wp-admin/post.php near line# 260
But thanks for this post once again!
As per Per’s comments above, this doesn’t work with Custom Post Types…any idea what might need to be done to get it to do so?
Hi All–sorry for my absence from this thread. I was working my way out of a heavy workload. Regarding custom post types, I believe the problem comes from this line:
if ( $post->post_type == 'page' )You want change ‘page’ to your custom post type or remove that if statement all together. I haven’t tested it, so maybe someone can fiddle around with it and report back with their code. Thanks!
Great bit of code!
I’ve been able to get it to work with the administrator role, but for some reason, the author role fails to trash the item. Rather than sending the post to the trash, I am forwarded to a ‘page not found’ page.
Any assistance appreciated!
Thanks!
Hi Guys, I modded the code to allow for custom post types and roles other than admin. I also added a trashed post handler to allow for redirect after the post is trashed if not in the admin area. Let me know if you find any bugs in the code, thanks!
And in response to Stephen, the “author” role does not have the capability to delete posts by default. You would need to add this capability to the author role if you wanted authors to be able to use this function. You can modify role capabilities via various plugins like Capability Manager.
Hello, very great plugin. Thank you. How’s that the link only see clearly the text for the post author and invisible to everyone?
Thanks for the help. greeting
Hi Rick, to make it so only the post author can see the delete post link, you would want to edit these lines:
The above is saying only users that can delete posts can see the link. To make it so only the author can delete the post, try working this code in instead of the above:
Hey Laura, thanks for that. It works so perfectly. But the users are reversed. So I do not see my ads on the link, but that of others. ? Could you please help me even off operations? Thank You Cheers Rick
Hey Laura,
this is working if the current user is admin, but the current user is author then this function is not working,
any suggestions , thanks in advance.