Created
March 26, 2022 05:10
-
-
Save eiiot/c2d11d16ab64e788eafd71e2474375a8 to your computer and use it in GitHub Desktop.
Wordpress - Migrate from User Profile Picture to Simple Local 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 | |
/* | |
Migrate from User Profile Picture to Simple Local Avatars | |
Allows sites to easily move away from the WP User Avatar plugin and switch to Simple Local Avatars instead. | |
Run by invoking with WP CLI like so: | |
`wp eval-file migrate-wp-user-avatar.php` | |
Author: Eliot Hertenstein (forked from Philip John) | |
Author URI: http://eliothertenstein.com | |
License: GPLv2 | |
Note: I take no responsibility for data loss throught the use of this script. Use at your own risk | |
*/ | |
// get the current image ID | |
$meta_key = 'wp_metronet_image_id'; | |
// 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. | |
$avatar_id = get_user_meta( $user->ID, $meta_key, true ); | |
// Attach the user and media with Simple Local Avatars. | |
$sla = new Simple_Local_Avatars(); | |
$sla->assign_new_user_avatar( (int) $avatar_id, $user->ID ); | |
Check that worked. | |
$sla_done = get_user_meta( $user->ID, 'simple_local_avatar', 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