• Resolved the1path

    (@the1path)


    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

Viewing 15 replies - 1 through 15 (of 16 total)
  • 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.

    Thread Starter the1path

    (@the1path)

    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

    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!

    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. 😉

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

    Thread Starter the1path

    (@the1path)

    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. 🙂

    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

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

    That is not what the1path is trying to do!

    He/she wanted to check if a user has a blog. I have been looking for the same thing.

    The code the1path provided works great. Thanks for posting!

    I put this in my author.php:

    $user_id = $curauth->ID;
    $key = 'primary_blog';
    $single = true;
    $primary_blog = get_user_meta( $user_id, $key, $single);
    
    if ($primary_blog == "") {?>
                        <p>No site yet</p>
              <?php } else { ?>
                   	<p>Member has site</p>
    <?php
    }

    $user_id depends on your specific case, but this seems like a pretty straightforward, solid solution.

    Is it? Am I missing something? Is there a more standard way to do this?

    but I dont think its that pretty

    Seems to do exactly what I needed with only a few lines!

    Thread Starter the1path

    (@the1path)

    Yeh my code definitely works but I dont think its that pretty. There is probably a WP function for this now?????

    One issue with the1path’s code; it still doesn’t check if the user is the administrator of the primary blog.

    Even if the user is only a subscriber on someone else’s blog, the code will still show ‘Member has site’.

    So for my case I have to add a check AND is not Administrator to this condition:

    if ($primary_blog == "")

    How can I do that? Probably something with user_can()?

    Searching for solution. Will report back…

    [ Moderator note: please wrap code in backticks or use the code button – http://codex.wordpress.org/Forum_Welcome#Posting_Code ]

    Hi Both,

    Sorry for confusion.

    Here is a approach I have used before. First you need to check if a user has any blogs. You can do this by:

    $user_blogs = get_blogs_of_user( $user_id );

    If $user_blogs is empty then they have no blogs.
    You can then check each blog returned.

    foreach user $user_blogs as $blog ...
    $blog_id = $blog->userblog_id;

    Then.. use use this function below for the blogs returned.

    function user_can_for_blog($user_id, $blog_id, $capability ) {
    
            $user = new WP_User( $user_id) ;
            $user->for_blog( $blog_id );
            $args = array_slice( func_get_args(), 2 );
            $args = array_merge( array( $capability ), $args );
            return call_user_func_array( array( &$user, 'has_cap' ), $args );
           }

    I tweaked an existing function. Returns boolean true or false if a user has a specific permission for a specific role on a specific blog.

    Hopefully you can use these components to test any assumptions.

    Best

    Jon

    jjones7, thanks for the suggestion, but I have no clue what is going on in your code. the1path’s code looks a lot simpler.

    Why does subscribing to someone else’s blog count as having a primary_blog? What is the point of having a key like that if it is not restricted to the user’s own blogs?

    Is there any way to incorporate user_can() into the1path’s code? Would ‘user can not’ be like this, like regular PHP:

    !user_can( $user, $capability )

    And then just use a random Administrator capability, like activate_plugins or manage_options?

    jjones7, what would $capability be in your code?

    This produces a server error (‘unexpected T_ELSE’):

    $user_id = $curauth->ID;
    $key = 'primary_blog';
    $single = true;
    $primary_blog = get_user_meta( $user_id, $key, $single);
    
    if ($primary_blog == "") {
    echo '<p>No site yet</p>';
    } else if (!user_can( $user_id, manage_options )) {
    echo '<p>No site yet</p>';
    else {
    echo '<p>Member has site</p>';
    }

    This causes the same error:

    if (!user_can( $user_id, manage_options )) {
    echo '<p>No site yet</p>';
    else {
    echo '<p>Member has site</p>';
    }

    So I guess !user_can( $user_id, manage_options ) is wrong.

    Can someone please help me figure out the right syntax etc?

    capability is user role such as editor, admin , contributor etc.

    You can check if a user has a specific role on a specific blog using the function I submitted.

    So what I am saying is that

    1./ you check if a user has any blog at all..

    $user_blogs = get_blogs_of_user( $user_id ));
    if !$user_blogs {
    echo 'User has no blog';
    }
    else {
    echo 'User has a blog';
    }

    2./ If they do have a blog, you can then check if they do (or dont) have a specific role on that blog. (i.e editor, admin etc) using

    function user_can_for_blog($user_id, $blog_id, $capability ) {
    
            $user = new WP_User( $user_id) ;
            $user->for_blog( $blog_id );
            $args = array_slice( func_get_args(), 2 );
            $args = array_merge( array( $capability ), $args );
            return call_user_func_array( array( &$user, 'has_cap' ), $args );
           }

    This produces several syntax errors:

    $user_blogs = get_blogs_of_user( $user_id ));
    if !$user_blogs {
    echo 'User has no blog';
    }
    else {
    echo 'User has a blog';
    }

    unexpected ‘)’ and ‘!’.

    It also still does not check if the user it the Administrator/Owner of a blog. This would still say the ‘User has a blog’ if he is only a Subscriber.

    Where would you get that $blog_id to do the second check? Is there no way to check if a user is Admin anywhere?

    Is there any way to incorporate a user_can_for_blog() check in the1path’s solution? Is $primary_blog the $blog_id?

    This works without server errors:

    $user_id = $curauth->ID;
    $key = 'primary_blog';
    $single = true;
    $primary_blog = get_user_meta( $user_id, $key, $single);
    
    if ($primary_blog == "") {
    echo '<p>No site yet</p>';
    } else if (user_can( $user_id, edit_posts )) {
    echo '<p>Member has site</p>';
    }

    But ‘Member has site’ only shows up for super admins. Nothing is echoed for regular members who definitely have sites.

    Based on jjones7’s suggestions this could work:

    $user_id = $curauth->ID;
    $key = 'primary_blog';
    $single = true;
    $primary_blog = get_user_meta( $user_id, $key, $single);
    
    if ($primary_blog == "") {
    echo '<p>No site yet</p>';
    } else if (user_can_for_blog($user_id, $primary_blog, edit_posts )) {
    echo '<p>Member has site</p>';
    }

    But user_can_for_blog() is an ‘undefined function’.

    So that user_can check does not work. Any alternatives?

    There is a user_can_for_blog() patch.

    Looks like this:

    function user_can_for_blog( $user, $blog_id, $capability) {
    	if ( ! is_object( $user ) )
    	$user = new WP_User( $user );
    
    	if ( ! $user || ! $user->ID )
    	return false;
    
    // Set the blog id.  @todo add blog id arg to WP_User constructor?
    	$user->for_blog( $blog_id );
    
    	$args = array_slice( func_get_args(), 3 );
    	die(print_r($args));
    	$args = array_merge( array( $capability ), $args );
    
    	return call_user_func_array( array( &$user, 'has_cap' ), $args );
    }

    But if I add that to author.php with the previous code, I get:

    Array ( ) 1

    user_can probably assumes we want to know the user’s capabilities for the main blog or blog ID 1.

    Stuck…

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Test to see if user is a site admin, can it be done in MU?’ is closed to new replies.