Last active
March 6, 2026 17:32
-
-
Save kimcoleman/0fe3effe0ea7c53a146f9d29559adcc3 to your computer and use it in GitHub Desktop.
UNTESTED: Cleanup script: Remove User Fields Avatar meta after PMPro migration.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Cleanup script: Remove User Fields Avatar meta after PMPro migration. | |
| * Use the migration script first: https://gist.github.com/kimcoleman/bab980d8eba1c59fc1be966eaa23a5d5 | |
| * | |
| * Run this as a WP-CLI command. | |
| * Save this code to a file cleanup-user-fields-avatars.php. | |
| * Put the file in your WordPress root directory (the same folder where wp-config.php lives). | |
| * | |
| * WP-CLI usage: wp eval-file cleanup-user-fields-avatars.php | |
| * Update the $dry_run value to "false" to run the actual cleanup. | |
| * | |
| * Note: This script is not automatically going to delete the media library files. | |
| * If you are certain you want to do that, update the $delete_media value (set to 'true') to handle deletion. | |
| */ | |
| $dry_run = true; | |
| if ( $dry_run ) { | |
| WP_CLI::log( '--- DRY RUN MODE — no changes will be made ---' ); | |
| } | |
| // Find all users who have the old User Fields avatar meta. | |
| $users = get_users( array( | |
| 'meta_key' => 'wp_user_avatar', | |
| 'meta_compare' => 'EXISTS', | |
| 'fields' => 'ids', | |
| 'number' => -1, | |
| ) ); | |
| if ( empty( $users ) ) { | |
| WP_CLI::log( 'No users with wp_user_avatar meta found. Nothing to clean up.' ); | |
| exit; | |
| } | |
| WP_CLI::log( sprintf( 'Found %d user(s) with wp_user_avatar meta.', count( $users ) ) ); | |
| $cleaned = 0; | |
| $skipped = 0; | |
| foreach ( $users as $user_id ) { | |
| // Safety check: only clean up if PMPro avatar migration succeeded. | |
| $pmpro_avatar = get_user_meta( $user_id, 'pmpro_avatar', true ); | |
| if ( empty( $pmpro_avatar ) ) { | |
| WP_CLI::warning( sprintf( 'User %d: no pmpro_avatar found — skipping (migration may have failed).', $user_id ) ); | |
| $skipped++; | |
| continue; | |
| } | |
| $attachment_id = (int) get_user_meta( $user_id, 'wp_user_avatar', true ); | |
| // Delete the Media Library attachment. | |
| // Note: leave $delete_media = false if anything else on your site references these attachments. | |
| $delete_media = false; | |
| if ( $delete_media && ! empty( $attachment_id ) ) { | |
| WP_CLI::log( sprintf( 'User %d: deleting media attachment %d.', $user_id, $attachment_id ) ); | |
| if ( ! $dry_run ) { | |
| wp_delete_attachment( $attachment_id, true ); | |
| } | |
| } else { | |
| WP_CLI::log( sprintf( 'User %d: leaving media attachment %d in place.', $user_id, $attachment_id ) ); | |
| } | |
| // Remove the wp_user_avatar meta (attachment ID reference). | |
| WP_CLI::log( sprintf( 'User %d: removing wp_user_avatar meta.', $user_id ) ); | |
| if ( ! $dry_run ) { | |
| delete_user_meta( $user_id, 'wp_user_avatar' ); | |
| } | |
| // Remove the user_avatar meta (raw User Field value — stores file path info). | |
| WP_CLI::log( sprintf( 'User %d: removing user_avatar meta.', $user_id ) ); | |
| if ( ! $dry_run ) { | |
| delete_user_meta( $user_id, 'user_avatar' ); | |
| } | |
| $cleaned++; | |
| } | |
| WP_CLI::log( sprintf( 'Cleanup complete. Cleaned: %d | Skipped: %d', $cleaned, $skipped ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment