WordPress distinguishes usermeta keys between sites by using the database prefix for each site.
For example, instead of using the favorite_posts key, you’d use the meta key wp_23_favorite_posts. To get the prefix, you can use $wpdb->get_blog_prefix()
.
But wait, there’s actually a whole API dedicated to this. Rather than using *_user_meta()
, use *_user_option()
. These are internally translated to be against the individual site.
And, it’s easily integrated into your existing plugin. get_user_option()
checks against a site-specific key first, but if it doesn’t find anything, it falls back to a user-wide meta key. So go ahead and switch to get_user_option()
and your existing plugin will work on single site without a problem.
Here are the function definitions:
./wp-includes/user.php:251:function get_user_option( $option, $user = 0 )
./wp-includes/user.php:293:function update_user_option( $user_id, $option_name, $new_value )
./wp-includes/user.php:322:function delete_user_option( $user_id, $option_name )