Created
September 13, 2022 11:34
-
-
Save grappler/d23d7232cc6e43b422a5bf30c722ab1f to your computer and use it in GitHub Desktop.
Allows sites to easily move away from the WP User Avatar plugin and switch to WP User Avatars instead.
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 | |
/* | |
Migrate from WP User Avatar to WP User Avatars | |
Allows sites to easily move away from the WP User Avatar plugin and switch to WP User Avatars instead. | |
Run by invoking with WP CLI like so: | |
`wp eval-file migrate-wp-user-avatar.php` | |
Author: Ulrich Pogson | |
Adapted from: https://gist.github.com/philipjohn/822d3521a95481f6ad7e118a7106fbc7 | |
License: GPLv2 | |
*/ | |
// Get the name of the meta key for WP User Avatar. | |
global $wpdb; | |
$meta_key = $wpdb->get_blog_prefix() . 'user_avatar'; | |
// Grab all users that have a local avatar. | |
$users = get_users( | |
[ | |
'meta_key' => $meta_key, | |
'meta_compare' => 'EXISTS', | |
] | |
); | |
foreach ( $users as $user ) { | |
// Get the existing avatar media ID. | |
$media_id = get_user_meta( $user->ID, $meta_key, true ); | |
if ( ! $media_id ) { | |
continue; | |
} | |
wp_user_avatars_update_avatar( $user->ID, (int) $media_id ); | |
// Check that worked. | |
$sla_done = get_user_meta( $user->ID, 'wp_user_avatars', true ); | |
if ( ! empty( $sla_done ) ) { | |
// Remove the old WP User Avatar meta data. | |
delete_user_meta( $user->ID, $meta_key ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment