Home How To How to Remove Featured Image When Deleting a WordPress Post

How to Remove Featured Image When Deleting a WordPress Post

by John Christopher
0 comment
A+A-
Reset
how to remove featured image when deleting a wordpress post

Are you thinking and looking for a method to remove featured image when deleting a wordpress post with wp_delete_attachment? While there’s probably a plugin for this, this quick code snippet has been created for you, you can use it to remove featured image when deleting a post in WordPress.

When you delete a post or page, the featured image that you’ve attached to it will remain there on your website. If you want to remove the featured image attachments as well while deleting the post or page, then keep in mind:

  • If the same featured image is attached to multiple posts, then permanently deleting any of those posts will delete the featured image as well.
  • The images will only be removed when you permanently delete the post from trash.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

add_action( 'before_delete_post', 'wps_remove_attachment_with_post', 10 );
function wps_remove_attachment_with_post($post_id)
{
 
    if(has_post_thumbnail( $post_id ))
        {
      $attachment_id = get_post_thumbnail_id( $post_id );
      wp_delete_attachment($attachment_id, true);
    }
 
}

The above will help you delete your wiordpress featured image from every of your posts you deleted and cleared your wordpress trash bin. please also kindly share this post to your friends if this post helped you to solve this issue.

banner
Disclosure: This website may contain affiliate links, which means I may earn a commission if you click on the link and make a purchase. I only recommend products or services that I personally use and believe will add value to my readers. Your support is appreciated!

Related Articles

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ngdrives blog logo

NGDrives Blog bring the latest tech news and tech tricks, get top 10 PHP Scripts and WordPress Templates & Setup Guides All for Free No Pay..

Buy Soledad now!

Edtior's Picks

Latest Articles

© Copyrights. 2024 – All Right Reserved. Powered by Soledad WP Theme

This website may contain affiliate links, which means I may earn a commission if you click on the link and make a purchase. I only recommend products or services that I personally use and believe will add value to my readers. Your support is appreciated!
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.