Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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,

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

share|improve this question
    
@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 –  booota Sep 13 '11 at 9:24
add comment

2 Answers

up vote 2 down vote accepted

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);
share|improve this answer
add comment

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.

share|improve this answer
    
Does wp_delete_post function delete the post, meta and attachments at once? if not what does it exactly deletes? –  booota Sep 13 '11 at 5:39
    
i just figured out that it unattaches the images from files and not deletes the attachments –  booota Sep 13 '11 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 Sep 13 '11 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? –  booota Sep 19 '11 at 8:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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