Delete Post From Front-End and attachment permanently

Is there a way to delete a Post From Front-End and it’s attachments permanently? This is a snippet that moves the post to the trash can, but it doesn’t remove attached images (they remain on the server) and it doesn’t remove the post permanently? Could someone please help?

<?php 
$url = get_bloginfo('url');
if (current_user_can('edit_post', $post->ID)){
echo '<a class="delete-post" href="';
echo wp_nonce_url("$url/wp-admin/post.php?action=delete&post=$id", 'delete-post_' . $post- 
>ID);
echo '">Delete post</a>';
}
?>

Related posts

Leave a Reply

2 comments

  1. Try:

    <?php if (current_user_can('edit_post', $post->ID)) echo "<a href='" . wp_nonce_url("/wp-admin/post.php?action=delete&amp;post=$id", 'delete-post_' . $post->ID) . "'>Delete post</a>" ?>
    

    You can decide when to empty the WordPress trash by adding this code to the wp-config.php file in your WordPress root directory.

    define('EMPTY_TRASH_DAYS', 1 );
    

    The 1 in the code signifies you want to empty the trash everyday. If you set to 0, the trash functionality will be disabled.

    Finally, WordPress doesn’t delete images when they are no longer attached to a page. See this ticket for an explanation: http://core.trac.wordpress.org/ticket/12108 Gist being that

    media files may be used by other posts
    as well, which is why they must be
    deleted in the media library. If we
    changed it so that deleting a file
    from a post deleted it altogether from
    the system, it would break the
    existing behavior and cause a lot of
    unintentional deletions.

    If you want to go against that rational, you can add this to your functions.php:

    function delete_post_children($post_id) {
        global $wpdb;
    
        $ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_parent = $post_id AND post_type = 'attachment'");
    
        foreach ( $ids as $id )
            wp_delete_attachment($id);
    }
    add_action('delete_post', 'delete_post_children');
    

    Also see Upload Janitor if you want to go the plug-in route for deleting unattached images.

  2. There is no need to call the database manually, this works too:

    add_action( 'delete_post', 'delete_post_children' );
    
    function delete_post_children( $post_id )
    {
        foreach( get_attached_media( '', $post_id ) as $attachment )
    
            wp_delete_attachment( $attachment->ID );
    }