Forums

[resolved] Test to see if user is a site admin, can it be done in MU? (8 posts)

  1. the1path
    Member
    Posted 10 months ago #

    OK so in plain english I want to code the following in WPMU:

    If current user has a blog then show banner

    So I thought perhaps the following WP method might work:

    if (current_user_can('administrator'))

    Ive also tried is_admin()

    but these only show a banner for the super network admin. I want it to show for just site admins.

    I had a quick look in the database and can see wp_user_level wp_2_user_level and wp_3_user_level does the current_user_can and is_admin function just link in with wp_user_level?

    How can I achieve this check to see if a user has a blog in my MU install? Do I need a custom sql query?

    Any help mucho appreciated

    Ta

  2. Andrea_r
    team pirate
    Posted 10 months ago #

    It's not wpmu uless you're running actual code from mu.wordpress.org.

    The code is no different than single WordPress.

    if (current_user_can('administrator'))

    This is invalid. See http://codex.wordpress.org/Roles_and_Capabilities for proper arguments.

  3. the1path
    Member
    Posted 10 months ago #

    Hi Andrea, thanks for the reply.

    Yeh I tried if (current_user_can('level_10')) first of all and that got the same unwanted result. But if you look at the codex here you see that they use if (current_user_can('administrator')) in the examples section, which is why I tried it: http://codex.wordpress.org/Function_Reference/current_user_can

    Anyway nothing works so I delved into the codex and the WP DB to see if I could come up with an answer. I saw that if a user had a blog or site they had extra fields in the wp_usermeta table, one of which is the met_key: primary_blog. So I thought if I could test a user against one of these fields I can see if they are an admin to a blog?

    I went round the houses a bit but eventually got my head together (after some sleep) and looked at: Get User Meta Function

    Here is the solution that seems to be working, can you see any problems with it Andrea or perhaps a better way of performing the test?:

    <?php if ( is_user_logged_in() ){ ?>
    	<?php
    	$current_user = wp_get_current_user();
    	$user_id = $current_user->ID;
    	$key = 'primary_blog';
    	$single = true;
    	$primary_blog = get_user_meta( $user_id, $key, $single);
    	if ($primary_blog == "") {
    	?>
                        <p>Display banner</p>
              <?php } else { ?>
                    	<p>Do nothing</p>
              <?php } ?>
             <?php } ?>

    Well it seems to work as I have two blogs under super admin with different logins and test user account with no blog. The banner is only displayed for the non blogging user.

    Hope this helps somebody and Im open to better suggestions (Im no coder)

    Cheers

  4. Tom Lynch
    Member
    Posted 10 months ago #

    If you are actually running WordPress MU (2.9 or lower) then you should update however what you are looking for in order to see if a user is a 'Site Admin' in old parlance is...

    is_site_admin( <em>$user_id</em> )

    Doc

    If you are running WordPress (3.0 or greater) with MultiSite enabled then you should use...

    is_super_admin( <em>$user_id</em> )

    Doc

    Hope this helps!

  5. Andrea_r
    team pirate
    Posted 10 months ago #

    he was checking to see if a user was an admin of a blog, or if they had a blog - not for super admin status. ;)

  6. Tom Lynch
    Member
    Posted 10 months ago #

    Sorry I misunderstood because he said site admin in the title which in MU is super admin

  7. the1path
    Member
    Posted 10 months ago #

    Yeh I was looking to test if a user was "a site admin". Im using WP 3.2.1 (stipulated in sidebar) and Buddypress.

    My solution seems to work so Ive marked thread as resolved but Im sure some expert can chime in with a more elegant solution. :)

  8. jjones7
    Member
    Posted 2 months ago #

    HI all,

    The best way of checking who the site owner is of a specific site..... is by

    1./ Retrieving the site admins email from the blog you want to check,

    $example_blog_id = 99;
    $admin_email = get_blog_option($example_blog_id, ‘admin_email’);

    2./ Getting the users info by that email..

    $user_data = get_user_by(‘email’, $admin_email);

    3./ and then checking against the admin data against the user data. You can check against anything that is unique..

    if ($user_data->user_email == $example_user_email_to_check_against) {
    do something here
    }

    Thanks

Reply

You must log in to post.

About this Topic