WordPress “Delete Post Link”

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 ) )
    $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:

function my_trashed_post_handler($post_id) {
   if (!is_admin()) {
    global $post;
    $user_info = get_userdata($post->post_author);

Tags: ,

26 Responses to “WordPress “Delete Post Link””

  1. Andy says:

    Nice, thanks!

    But it outputs only a text, no link at all?

  2. Laura McDonald says:

    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.

  3. Hi I am getting the same problem. It just outputs the text ‘Trash’ but it is not clickable.

    Any ideas? great idea!

  4. Laura McDonald says:

    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…

  5. marco says:

    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.

  6. Brandon says:

    Thanks! This worked perfectly for my client. He wants to manage the site without having to go into the backend :/

  7. Per says:


    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.

  8. Laura McDonald says:

    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.

  9. Per says:

    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..

  10. Magneto says:

    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.

  11. Laura McDonald says:

    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.

  12. AnyDog says:

    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 ?

  13. valuser says:

    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.

  14. shahmmer says:


    How can I edit this so it also allows non registered users to delete posts?


  15. William says:

    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!

  16. Peer says:

    It works gr8 for me , but in a single post(inner page0 redirection after deleting is not proper,
    Any idea will be highly appriciable.

  17. Peer says:

    Hey I resolved the above redirection problem;
    just add

    //wp_redirect( add_query_arg('deleted', 1, $sendback) );
      wp_redirect( add_query_arg('deleted', 1, get_bloginfo('wpurl') ) );


    in wp-admin/post.php near line# 260

    case 'delete': // section
    Resolve :0 

    But thanks for this post once again!

  18. Nathan says:

    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?

  19. Laura McDonald says:

    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!

  20. Stephen says:

    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!


  21. Laura McDonald says:

    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!

  22. Laura McDonald says:

    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.

  23. Rick says:

    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

  24. Laura McDonald says:

    Hi Rick, to make it so only the post author can see the delete post link, you would want to edit these lines:

    if ( !current_user_can( $post_type_object->cap->delete_post, $post->ID ) )

    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:

    global $post,$current_user;
      if ($post->post_author == $current_user->ID)

  25. Rick says:

    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

  26. noor says:

    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.

Leave a Reply

Preserve code formatting within <code> and <pre> tags

What is 5 + 4 ?
Please leave these two fields as-is:
Please help us combat comment spam by answering this question.