Last active
April 28, 2022 09:06
-
-
Save philipjohn/822d3521a95481f6ad7e118a7106fbc7 to your computer and use it in GitHub Desktop.
This file contains 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 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: Philip John | |
Author URI: http://philipjohn.me.uk | |
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. | |
$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 ); | |
} | |
} |
I get a fatal error:
Fatal error: Uncaught Error: Call to a member function get_blog_prefix() on null in /public_html/wp-content/migrate-wp-user-avatar.php:18 Stack trace: #0 {main} thrown in /public_html/wp-content/migrate-wp-user-avatar.php on line 18
Anyone who can help?
@philipjohn I received a report of this failing on multisite, where the script only works on the main site and not subsites. Is that a known issue, something that needs to be accounted for in running the script differently, or a user mistake?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this code, but how to make this work for custom user profile photo it uses cupp_upload_meta and cupp_meta i tried to change but without success please advise.
update: I had to remove $wpdb->get_blog_prefix() . to make it working but all new values for simple_local_avatar its return as the following a:2:{s:8:"media_id";i:0;s:4:"full";b:0;}