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 = '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;
}
$message = "Are you sure you want to delete ".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>'); ?>


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!