News

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 = '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>'); ?>

Tags: ,

19 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:

    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.

  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:

    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 ?

  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:

    Hi,

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

    Thanks

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

  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') ) );
    

    to

    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!

Leave a Reply

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