I used

// Delete Post Link
function wp_delete_post_link($link = 'Delete This', $before = '', $after = '', $title="Move this item to the Trash", $cssClass="") {
    global $post;
    if ( $post->post_type == 'page' ) {
        if ( !current_user_can( 'edit_page' ) )
    } else {
        if ( !current_user_can( 'edit_post' ) )
    $delLink = wp_nonce_url( site_url() . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'trash-' . $post->post_type . '_' . $post->ID);
    $link = '<a class="' . $cssClass . '" href="' . $delLink . '" onclick="javascript:if(!confirm(\'Are you sure you want to move this item to trash?\')) return false;" title="'.$title.'" />'.$link."</a>";
    return $before . $link . $after;

to add a delete post link in my custom theme,

<?php echo wp_delete_post_link(); ?>

but for some reason it is not deleting all the attached info. Only the post is deleted. I checked the Media Library and the attachments were still there.

How can I implement to delete everything linked to the post?

Note that I am implementing this on the front end and not the admin side.

Ok additionally, i added action for delete_post and in that called wp_delete_attachment function to delete the attachments. But the delete link generated by above mentioned tutorial is giving wp failure page due to invalid nonce calculated. But it works perfect if i change action to trash. I need delete to be working. Can anyone help? sample image

  • @Rarst: Ok keeping in mind the above additional info what solution comes to your mind? I have tried playing with nonce calculation and got no happy ending... :( Though the code is working if i change delete_post to trash_post and in the link action=trash Commented Sep 13, 2011 at 9:24

2 Answers 2


Try using these delete links instead. Yours don't seem to be formatted right.

For delete:

$delLink = wp_nonce_url( admin_url() . "post.php", "post=" . $post->ID . "&action=delete");

For trash:

$delLink = wp_nonce_url( admin_url() . "post.php", "post=" . $post->ID . "&action=trash");


No try this instead:

$post_type = get_post_type($post);
$delLink = wp_nonce_url( admin_url() . "post.php?post=" . $post->ID . "&action=delete", 'delete-' . $post_type . '_' . $post->ID);

Attachments are not considered part of the post, because they can be easily used in multiple places.

Deleting them would be considerably more complex than solution from that tutorial. You will need to put together custom handler for your link (as Ajax action or something) and inside that handler retrieve attachments, associated with a post, and then delete them together with post itself.

  • Does wp_delete_post function delete the post, meta and attachments at once? if not what does it exactly deletes? Commented Sep 13, 2011 at 5:39
  • i just figured out that it unattaches the images from files and not deletes the attachments Commented Sep 13, 2011 at 5:52
  • @booota wp_delete_post() will delete post itself, its custom fields and such. wp_delete_attachment() on attachment post will delete that attachment and all of its media.
    – Rarst
    Commented Sep 13, 2011 at 8:57
  • I am trying to use delete_post action hook with the wp_delete_post() function but for some reason it does not seem to be working. Any idea? Commented Sep 19, 2011 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.