Skip to main content

Invalidate caches or sync to external systems when a subscriber removes their avatar

Premium Beginner

A subscriber removing their photo is deleting something about themselves from your site, and they expect it to be gone. If your add-on copied that image anywhere — a cache, a CDN, an external profile — it is not gone, and the subscriber has no way to know.

benecaster_avatar_deleted fires once the removal is complete, and tells you which subscriber and which image, so you can clear your own copies.

It does not fire when there was nothing to remove. A delete request from a subscriber who had no photo is treated as already done, so your callback only ever runs on a real deletion. Pair it with benecaster_avatar_uploaded to handle both directions.

Code

<?php
add_action( 'benecaster_avatar_deleted', function ( int $user_id, int $attachment_id ): void {
    wp_cache_delete( 'avatar_url_' . $user_id, 'my-addon' );
    my_crm_clear_profile_picture( $user_id );
}, 10, 2 );

View on GitHub →

Hooks Used

Need this built rather than just documented? See our services →