Remove featured image when deleting post

Code Snippets

Remove featured image when deleting post –

Questo frammento di codice elimina automaticamente l’immagine di anteprima dalla libreria multimediale di WordPress quando si elimina un articolo.

Come funziona ?

Basta copiare ed incollare il codice sottostante nel file function.php del vostro tema e l’immagine in evidenza verrĂ  automaticamente cancellata dalla vostra libreria multimediale.
add_action( 'before_delete_post', 'wp_remove_attachment_with_post', 10 );
function wp_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);


Nota importante: eseguite prima un backup del file functions.php, per poterlo ripristinare in caso di problemi.

Our website uses cookies and thereby collects information about your visit to improve our website (by analyzing), show you Social Media content and relevant advertisements. Please see our cookies page for furher details or agree by clicking the 'Accept' button.

Cookie settings

Below you can choose which kind of cookies you allow on this website. Click on the "Save cookie settings" button to apply your choice.

FunctionalOur website uses functional cookies. These cookies are necessary to let our website work.

AnalyticalOur website uses analytical cookies to make it possible to analyze our website and optimize for the purpose of a.o. the usability.

Social mediaOur website places social media cookies to show you 3rd party content like YouTube and FaceBook. These cookies may track your personal data.

AdvertisingOur website places advertising cookies to show you 3rd party advertisements based on your interests. These cookies may track your personal data.

OtherOur website places 3rd party cookies from other 3rd party services which aren't Analytical, Social media or Advertising.