The MU forums have moved to WordPress.org

Tricky Problem - Get user_id of current blog? (20 posts)

  1. MrBrian
    Member
    Posted 14 years ago #

    I'm really in a bind. What i am trying to do is retrieve user information of the blog owner via the theme. Originally I had thought i could get the user_id by pulling it from user_meta like so:

    $current_user_id = $wpdb->get_var( "SELECT user_id FROM $wpdb->usermeta WHERE (meta_key = 'source_domain' AND meta_value = '$domain')" );
    $user = get_userdata( $current_user_id );

    This was working fine at first... but i recently realized that this would not work for multiple blogs because source_domain only stores the primary blog. I don't get why this is so hard. This HAS to be outside of a loop too. The thing about using the loop is you pull the current "authors" information and not the actual blog owners, plus you can't use it before the loop is initialized. The other option i was thinking about was using blog meta (get_blog_option), but that info is stored for each blog (which would be kind of silly when you're saving data for a user account). I guess i would be forced to do it that way, but it seems like this is lacking functionality for wordpress.

    I've searched and searched thru core functions and stared at the database for TOO many hours trying to find a correlation between the user and the blog so that you can pull the user_id. From the user_id.. you can obviously get everything you need. A Guru like Andrea, Luna, DrMike, or Andrew must know something about this! Some of you know i contribute here and there on these forums, so I greatly appreciate your help with this.

  2. andrea_r
    Moderator
    Posted 14 years ago #

    "retrieve user information of the blog owner via the theme"

    What for? Any chance you're going backwards? :)

  3. MrBrian
    Member
    Posted 14 years ago #

    When a blog is viewed there are only two global variables: $blog_id and $domain, hence the reason my code above uses $domain to pull the user_id of the blog owner. With the core functions the only user_id you can pull is for the currently logged in user, which is pretty much how ALL of the core does it (cookies). Have I drove myself crazy and missed something?

    My plugin basically assigns a special "code" to each user that signs up for a blog. I need to pull that code in the theme.

  4. andrea_r
    Moderator
    Posted 14 years ago #

    Try

    get_currentuserinfo()

  5. MrBrian
    Member
    Posted 14 years ago #

    That will pull the user data for the currently logged in user, not the owner of the blog. Not as easy as you thought, is it.

  6. lunabyte
    Member
    Posted 14 years ago #

    It is a bit more involved, but I do it on my sites.

  7. MrBrian
    Member
    Posted 14 years ago #

    The only way I can think of doing it is assigning the info to the blog's meta during blog creation. Is that how you do it, luna?

  8. lunabyte
    Member
    Posted 14 years ago #

    No, that's not how I have mine set up.

  9. MrBrian
    Member
    Posted 14 years ago #

    Could you elaborate a little bit on how you do it? I've really hit a wall on the right way to accomplish it...

  10. quenting
    Member
    Posted 14 years ago #

    Your problem is there is not a 1-1 relation between users and blogs. One blog can have multiple users, one user can have multiple blogs. So the notion of blog owner is fuzzy, and "the user id" you're talking about is not well defined.
    If you're within the post loop, it's easier, one post only has one author, so you can access this information with stuff like get_post_author etc.
    If you're within the sidebar or outside the loop though, it's more complex. You first need to figure what it is you want to get. Is it who created the blog originally (knowing that this person might not be the actual administrator of the blog anymore), who is the admin (there might be multiple ones), who has this blog as primary blog (there might be no one)...
    Depending on what actual user you want to retrieve, you'll need to fetch from wp_usermeta either primary_blog, or match wp_XXX_capabilities to 'a:1:{s:13:"administrator"; b:1;}' (man i wish wordpress devs would stop using serialization), or source_domain...

    You might not find an appropriate solution and have to use tricks. For instance, we have an "author avatar" sidebar module on unblog.fr that is confronted to this exact same problem of author notion not being that clear. What we do is *when the user picks this module* to add to his side bar, we store the user_id of whoever did it, and we use it to fetch his avatar, regardless of other considerations. It's just an example of a way around the problem. If you're developping an extra functionnality (which is likely), you might want to store the uid of whoever activates this functionnality at the time he does.

  11. MrBrian
    Member
    Posted 14 years ago #

    Ok i see what you're saying... i didn't think about the fact that blogs can have multiple owners (administrators). I'm thinking the only solution then would be to save the information to the blogmeta. You're correct i am adding functionality. During signup, i'm actually storing a specially generated code along with the uid of the user in a table of it's own. When the blog is viewed, the user's generated code needs to be pulled for displaying (kind of like an adsense code, but it's not). The code is specific to each user.. so it just made sense to me that it should be stored against the user (and not the blog). This has opened me to see a few possible solutions:

    1. Use the blog meta and store this code for each blog.

    2. Prevent users from registering more than one blog and simply pull the userid based on source_domain like i originally did. (i wouldn't use primary_blog because each blog can have many people that have it as their primary_blog)

    3. Pull using author information inside of the loop.

    4. The way of your author avatar plugin: Unsure as to how because you mention using the userid to fetch the avatar, but how do you find the userid in the first place?

    Regardless, i see now that the blogmeta is the best choice for me. Considering the versatility of wordpress blogs being able to have multiple owners, blogs, and authors. This was a tricky problem that hopefully becomes easier as wordpress grows. Thanks quenting for helping make it clear to me, really appreciate it.

  12. quenting
    Member
    Posted 14 years ago #

    4. The way of your author avatar plugin: Unsure as to how because you mention using the userid to fetch the avatar, but how do you find the userid in the first place?

    Since it's the user id of the user which adds the avatar to the sidebar module, you can use the current_user info.
    Indeed if your entity is associated more with the motion of blog than user, just store it in the blog options.
    Always keep in mind, since it's getting truer with erach version of WP that blogs and users are becoming more and more separate entities, with n-n relationships between the two. I remember the first time we were confronted to that problem, because in our original mu alpha blog_id was equal to user_id, and at some stage the ids got different and we had to fix how we linked the two. Nowadays you should always develp functions with regards to many users having many blogs. It can even get worse sometimes (like user creates a blog, then another one, then deletes the first, then gets given some rights on another blog that also becomes his primary, that's a good trick situation to test your devs).

  13. bschwarting
    Member
    Posted 14 years ago #

    i'm running into the same issue, and have been trying to tie them together as well. the multiple blogs issue makes sense as to why this is not easy.

    what about using wp_blogs(domain) and wp_users(user_login)?

    strip off the ending in wp_blogs (domain.com) and compare it to wp_users(user_login) ?

    if they aren't = then you know it's not a user_login, and a sub-blog

    if they are, you know it's a legit user_login

    not sure if that logic makes sense or not, thoughts?

  14. MrBrian
    Member
    Posted 14 years ago #

    At first i didn't understand what you were meaning (those wp_users and wp_blogs aren't functions).. but i think i understand what you're thinking. Pulling the user_login from the domain name.

    Flaws in this:
    The domain name will not always contain the blog owner's username for a few reasons:
    -A user can have no blogs that contain their user_login in the domain.
    -A blog could be transfered over to another user, totally confusing the situation.
    -

    Then, even if you figure out it's a sub-blog, you don't know who the owner is. I'm still stuck with having to save the information to the blog. Because of how much wordpress separates the blogs from the user, that's the best solution at the moment i think.

  15. bschwarting
    Member
    Posted 14 years ago #

    i wrote up the code for my solution, and it seems to be working for now :) i do see the holes in it though, just don't see another easy option. right now I'm doing a bunch of error checking to deal with the holes. my extra functionality will only be allowed on the primary blog. off to close some more holes!

  16. MrBrian
    Member
    Posted 14 years ago #

    I still don't see how that would work for you. When a user signs up, they CHOOSE their username and they CHOOSE their domain name. They could be entirely different...

  17. bschwarting
    Member
    Posted 14 years ago #

    doh, didn't think about that. my site is so new, everyone that has signed up has left it default, the same as the username, so i missed that.

    i tried to disable the input for the blog domain, so they can't change it, but it won't let the signup continue with it being grayed out.

    i changed the input to hidden, and that seems to go through fine.

    i hate doing this though, not giving them the option.

  18. bschwarting
    Member
    Posted 14 years ago #

    i put the input in twice, the 1st is disabled, so the user can still see what domain name they will get, and also a 2nd input that is hidden, so it still gets sent.

  19. ayao
    Member
    Posted 14 years ago #

    I recently found myself needing to print out a blog author's "About Yourself" in the sidebar. I did so by getting a list of the userids for each blog using get_users_of_blog() (from users.php), and then checking their permissions. Code I used (with widget bits removed):

    $user_list = get_users_of_blog();
    foreach ( $user_list as $user ) :
        $author = new WP_User($user->user_id);
        if ( $author->has_cap( "publish_posts" ) ) :
            if( $author->user_login == 'admin' && !$options['admin'] ) continue;
             echo "$before_desc $author->description <!-- $author->display_name -->$after_desc";
             endif;
        endif;
    endforeach;

    It seems to work okay, but based on the comments here, I've begun to wonder whether there are obvious pitfalls to doing it this way that I haven't thought of (other than that it won't catch past authors; only current ones). Any ideas?

  20. MrBrian
    Member
    Posted 14 years ago #

    If there's multiple administrators, that's going to output twice. But it's very unlikely for wpmu to have two administrators i think. Your solution is fine, but for me i needed a solution that would go outside the loop and not conflict at all. Hence, i have stuck with using blog meta.

About this Topic