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.

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>';
}
?>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
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.