Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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:
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 );
Copy link

Edotrix commented Jun 20, 2021

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;}

Copy link

Mahyulan commented Jul 14, 2021

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?

Copy link

jeffpaul commented Aug 20, 2021

@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