Skip to content

Instantly share code, notes, and snippets.

@grappler
Created September 13, 2022 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grappler/d23d7232cc6e43b422a5bf30c722ab1f to your computer and use it in GitHub Desktop.
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.
<?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