5

I've written a plugin that allows users to bookmark their favorite posts, but have run into a small problem when using it on a multisite network.

Each time a post is bookmarked, the ID of that post is stored in an array of IDs in the current user's meta table. This all works great, until it's used on a MS setup.

If viewing the site that the bookmarks were created on, everything works great, but when viewing a different site, I get a list of post IDs that belong to the other site. This is because the user meta table is shared between sites.

So, what I need to do is retrieve only the user meta for the current site. Is there a way to do this? I don't think there is, so if anyone has some insight, that'd be great.

19
+50

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 )
  • Oh, cool! I didn't know about get_user_option(). I will test it and let you know. – Pippin Nov 16 '11 at 15:58
  • what is interesting is that if I want to delete a site wide piece of user meta I must use delete_user_meta() because if I use delete_user_option() it prefixes the option with 'wp_' - which I'm guessing is the default site prefix. – Jason Oct 29 '13 at 15:33
  • Seems this person wants only the user meta for a specific site. How is using get_user_option supposed to help when it falls back to a user-wide meta key? user-wide here soulds like the full network. But don't want the network, just a site. Don't want the fallback here. So this seems like it does not help any. – Solomon Closson Sep 19 '17 at 19:13
4

why not store the blog id together with the array of postIds, so you will have something like this stored in the user meta data:

Array
(
   [blogid1] => Array(1,2,4,7),
   [blogid2] => Array(3,6,8,10)
)

you can use the global $blog_id to get the current blogid. On a non multisite setup, the blogid will be 0 and should still work when you try to get the posts out the array by

arr[$blog_id]

  • This is what I've been considering doing. The only issue I have with it is that it won't work with users who have already been using the plugin, though I suppose I could just write an upgrade function that would move all of the currently stored bookmarks into the new array structure. – Pippin Nov 14 '11 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.