By: Prem Tiwari | Last Updated:
Are you a WordPress developer or running a WordPress website for your business, then keep your close attention in this post as I am going to explain how to delete featured image when deleting a post using
Just assume if you are running a WordPress based blog and you have added some posts which contain images and after some time you deleted them, then your images are not deleted from your server and these all unused images and files are contains your server spaces and force your website to load slowly.
It is a better idea to remove the featured image during the post deletion time. In this, I will explain how to delete featured images using a WordPress hooks.
Copy the below code snippets and add it at the end of the functions.php file from you current activated theme.
add_action( 'before_delete_post', 'wps_remove_attachment_with_post', 10 );
if(has_post_thumbnail( $post_id ))
$attachment_id = get_post_thumbnail_id( $post_id );
Hope this tutorial help you, Do like and share this tutorial with your friends and also subscribe my blog to get my best tutorial directly in your email inbox. If you have any queries or suggestion regarding this tutorial, then feel free to put your comments in the below comment section.
Prem Tiwari is the founder of freewebmentor.com and is a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby and also he has been a regular speaker of WordPress sessions in various IT Companies.View all posts by Prem Tiwari