
Typically, when creating a post in WordPress, images are also attached. However, often when a post is deleted, the images remain. Today, we will take an in-depth look at the topic of “Automatic deletion of post images.”
Reasons why images are not deleted even if the post is deleted
The reason images remain even after a post is deleted is that images and posts are handled differently in the database. When a post is deleted, WordPress deletes the metadata and content of the post, but the associated images are treated as attachments managed separately. Therefore, by default, the images remain.
This creates a problem because if images related to a post continue to exist, it can waste storage space and cause issues such as slowing down the site.
Deleting unnecessary images from the Image Library (Media Library) each time can be cumbersome, and as the number of posts increases, it becomes difficult to manage.
Introducing a function for automatic deletion of post images
Below is the function code to delete all images associated with a post when it is deleted.
function delete_images_with_post( $post_id ) {
// Ignore revisions
if ( wp_is_post_revision( $post_id ) ) {
return;
}
// Delete featured image
$thumbnail_id = get_post_thumbnail_id( $post_id );
if ( $thumbnail_id ) {
wp_delete_attachment( $thumbnail_id, true );
}
// Delete all images attached in the content
$attachments = get_attached_media( 'image', $post_id );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID, true );
}
}
}
add_action( 'before_delete_post', 'delete_images_with_post' );
How the function works
The above function is connected to the action hook called `add_action( ‘before_delete_post‘, ‘delete_images_with_post’ );`. This hook is executed just before a post is deleted, triggering the ‘delete_image_with_post‘ function to find and delete the images associated with the post.
1. **Ignore revisions**: The function first checks if the deleted post is a revision. Revisions are previous versions automatically created every time a post is modified, so this business logic skips unnecessary revisions.
2. **Delete featured image**: The function uses `get_post_thumbnail_id` to find the featured image associated with the post. If a featured image exists, it is deleted using the `wp_delete_attachment` function.
3. **Delete all images in the content**: The function uses `get_attached_media` to find all images attached in the content. It specifies the type ‘image’ to extract all image formats, then executes a loop to delete each image.
Introduction to action hooks used and related hooks
The action hook `before_delete_post` is executed just before a post is deleted, allowing you to define and call a function for the functionality you want to execute when this hook operates. Many plugins that enhance WordPress functionality also implement features using such ‘hooks‘.
Similar action hooks
1. **after_delete_post**: This hook is executed after a post is deleted. It is useful for follow-up tasks such as tracking or logging.
2. **delete_attachment**: This hook occurs when an attachment is deleted. It can be used for the functionality of deleting images.
3. **before_delete_attachment**: This is executed before an attachment is deleted, allowing you to prevent deletion or perform additional tasks based on specific conditions.
How to use it?
Simply add the above code to the functions.php file in your theme folder. For more information on modifying functions.php, please refer to the article below.
Once the above code is added to functions.php, when you delete a post, all images included in the post will also be deleted. (They will be removed from the database records and the image files stored on the server will also be deleted.)
Notes
- It does not work when ‘deleted’ by sending to the ‘trash’. It works when ‘permanently deleted’.
- If the same image is used in other posts, it will not be displayed in the other posts. It is advisable not to use the same image across multiple posts.
In conclusion
The method for automatically deleting images associated with posts is very beneficial and greatly helps in blog management. By writing this code, you can maximize the efficiency of the database and improve the overall performance of the site.
The issue of images remaining when deleting posts can be bothersome, but applying the above method can easily solve it. When implementing it, simply add the code to the functions.php file for a super simple application!
[wp_is_post_revision] : A function that checks if the post is a revision, skipping specific operations if it is a revision.
[get_post_thumbnail_id] : A function that retrieves the ID of the featured image of a specific post.
[wp_delete_attachment] : A function that deletes a specific attachment, with the second parameter setting whether to force deletion.
[get_attached_media] : A function that retrieves a list of media attached to a specific post.