Delete Post Link to delete post, its meta and attachments

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' ) )
            return;
    } else {
        if ( !current_user_can( 'edit_post' ) )
            return;
    }
    $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,

Read More
<?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

Related posts

Leave a Reply

2 comments

  1. 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");
    

    EDIT:

    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);
    
  2. 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.