User Meta Prefix issue and WP 5.1 Compatibility
-
This plugin is great, but I recently found 2 issues with it:
- WP 5.1 depreciated the wpmu_create_blog action so the plugin should get updated. I modified my copy to use the new wp_initialize_site action easily.
- The code to copy the user meta looks only for the DB prefix _ blog_id – it doesn’t check for the trailing _ after blog_id, so if you have a default blog with ID = 2, it starts matching against blog_id = 20, 21, 22 etc leading to enormous duplication of data. I ended up with 27,000 records in my usermeta table very quickly (when it was only supposed to be duplicating 5 records). This leads to huge problems quickly. Here’s the code I used to fix the issue:
// Copy users if ( $copy_users ){ $users = get_users('blog_id='.$id_default_blog); $default_prefix = $prefix . $id_default_blog . '_'; $default_prefix_len = strlen($default_prefix); $new_prefix = $wpdb->get_blog_prefix(); function user_array_map( $a ){ return $a[0]; } foreach($users as $user){ $all_meta = array_map( 'user_array_map', get_user_meta( $user->ID ) ); foreach ($all_meta as $metakey => $metavalue) { if(substr($metakey, 0, $default_prefix_len) == $default_prefix) { $raw_meta_name = substr($metakey,$default_prefix_len); update_user_meta( $user->ID, $new_prefix . $raw_meta_name, maybe_unserialize($metavalue) ); } } } }
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘User Meta Prefix issue and WP 5.1 Compatibility’ is closed to new replies.