Created
March 6, 2026 17:15
-
-
Save kimcoleman/bab980d8eba1c59fc1be966eaa23a5d5 to your computer and use it in GitHub Desktop.
UNTESTED: Migration script: PMPro User Fields Avatar (wp_user_avatar) → PMPro Avatars
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 | |
| /** | |
| * Migration script: PMPro User Fields Avatar (wp_user_avatar) → PMPro Avatars | |
| * | |
| * Run this as a WP-CLI command. | |
| * Save this code to a file migrate-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 migrate-user-fields-avatars.php | |
| * | |
| * When ready to run for real, set $dry_run = false below. | |
| */ | |
| $dry_run = true; | |
| if ( $dry_run ) { | |
| WP_CLI::log( '--- DRY RUN MODE — no changes will be made ---' ); | |
| } | |
| // Find all users with a wp_user_avatar meta value set. | |
| $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 migrate.' ); | |
| exit; | |
| } | |
| WP_CLI::log( sprintf( 'Found %d user(s) to migrate.', count( $users ) ) ); | |
| $success = 0; | |
| $skipped = 0; | |
| $failed = 0; | |
| foreach ( $users as $user_id ) { | |
| // Skip users who already have a PMPro avatar — don't overwrite. | |
| $existing = get_user_meta( $user_id, 'pmpro_avatar', true ); | |
| if ( ! empty( $existing ) ) { | |
| WP_CLI::log( sprintf( 'User %d: already has pmpro_avatar, skipping.', $user_id ) ); | |
| $skipped++; | |
| continue; | |
| } | |
| // Get the attachment ID. | |
| $attachment_id = (int) get_user_meta( $user_id, 'wp_user_avatar', true ); | |
| if ( empty( $attachment_id ) ) { | |
| WP_CLI::warning( sprintf( 'User %d: wp_user_avatar meta is empty, skipping.', $user_id ) ); | |
| $skipped++; | |
| continue; | |
| } | |
| // Resolve the attachment to a filesystem path. | |
| $attached_file = get_attached_file( $attachment_id ); | |
| if ( empty( $attached_file ) || ! file_exists( $attached_file ) ) { | |
| WP_CLI::warning( sprintf( | |
| 'User %d: attachment %d file not found on disk (%s), skipping.', | |
| $user_id, | |
| $attachment_id, | |
| $attached_file | |
| ) ); | |
| $skipped++; | |
| continue; | |
| } | |
| WP_CLI::log( sprintf( 'User %d: migrating from attachment %d (%s)', $user_id, $attachment_id, $attached_file ) ); | |
| if ( $dry_run ) { | |
| $success++; | |
| continue; | |
| } | |
| // Determine extension from the attachment file. | |
| $filetype = wp_check_filetype( $attached_file ); | |
| $ext = ! empty( $filetype['ext'] ) ? $filetype['ext'] : 'jpg'; | |
| $save_ext = pmpro_avatar_get_save_extension( $ext ); | |
| // Set up the PMPro user avatar directory. | |
| pmpro_avatar_setup_directory(); | |
| $user_dir = pmpro_avatar_get_upload_dir( $user_id ); | |
| if ( ! file_exists( $user_dir ) ) { | |
| wp_mkdir_p( $user_dir ); | |
| } | |
| // Process the image — crop to square, resize to max dimension. | |
| $max_dimension = pmpro_avatar_get_max_dimension(); | |
| $image = wp_get_image_editor( $attached_file ); | |
| if ( is_wp_error( $image ) ) { | |
| WP_CLI::warning( sprintf( 'User %d: could not open image editor — %s', $user_id, $image->get_error_message() ) ); | |
| $failed++; | |
| continue; | |
| } | |
| $size = $image->get_size(); | |
| $min_dim = min( $size['width'], $size['height'] ); | |
| $crop_x = ( $size['width'] - $min_dim ) / 2; | |
| $crop_y = ( $size['height'] - $min_dim ) / 2; | |
| $image->crop( $crop_x, $crop_y, $min_dim, $min_dim ); | |
| if ( $min_dim > $max_dimension ) { | |
| $image->resize( $max_dimension, $max_dimension, true ); | |
| } | |
| $image->set_quality( 90 ); | |
| $base_path = $user_dir . 'avatar.' . $save_ext; | |
| $saved = $image->save( $base_path ); | |
| if ( is_wp_error( $saved ) ) { | |
| WP_CLI::warning( sprintf( 'User %d: failed to save processed image — %s', $user_id, $saved->get_error_message() ) ); | |
| $failed++; | |
| continue; | |
| } | |
| // Pre-generate bucket sizes. | |
| $current_size = $image->get_size(); | |
| $base_dim = $current_size['width']; // Square after crop. | |
| foreach ( pmpro_avatar_get_bucket_sizes() as $bucket ) { | |
| if ( $bucket < $base_dim ) { | |
| $bucket_image = wp_get_image_editor( $base_path ); | |
| if ( ! is_wp_error( $bucket_image ) ) { | |
| $bucket_image->resize( $bucket, $bucket, true ); | |
| $bucket_image->set_quality( 90 ); | |
| $bucket_image->save( $user_dir . sprintf( 'avatar-%dx%d.%s', $bucket, $bucket, $save_ext ) ); | |
| } | |
| } | |
| } | |
| // Save PMPro avatar meta. | |
| update_user_meta( $user_id, 'pmpro_avatar', array( | |
| 'extension' => $save_ext, | |
| 'uploaded' => time(), | |
| ) ); | |
| WP_CLI::log( sprintf( 'User %d: migrated successfully.', $user_id ) ); | |
| $success++; | |
| } | |
| WP_CLI::log( sprintf( | |
| 'Migration complete. Success: %d | Skipped: %d | Failed: %d', | |
| $success, $skipped, $failed | |
| ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment