Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment