I am trying to use the following code to 1st check if a user has a certain member level, then if they have a blog on the wp network. If they pass both those checks then a link is echoed, if they dont pass the first if check then another link is echoed. Also though, I am trying to check if they pass the first if but fail the second one then a different link is echoed. Here’s the code I have now –
<?php
if(pmpro_hasMembershipLevel(array(2,4))) {
if(current_user_can( 'edit_posts' )) {
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
if($blogs) {
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li><a href="http://' . $blog->domain . $blog->path .'wp-admin/">My Site</a></li>';
} else {
echo '<li><a href="/register-site/">Register your Site</a></li>';
}
}
}
}
} else {
echo '<li><a href="/membership-levels/">UPGRADE</a></li>';
}
?>
The code above echoes the register link when its suppose to but when the user has a blog, the register link shouldnt show but now it shows next to my site link. Any ideas?
EDIT
-
Free user sees a UPGRADE link
-
Premium Users without site see a REGISTER Link ( the membership array of 2,4 are the levels they have to be either one of )
-
Premium members with a site will see the MY SITE link.
EDIT
I was able to use the print_r and on the page where it’s suppose to echo the register link — Array ( [1] => stdClass Object ( [userblog_id] => 1 [blogname] => mysite.com [domain] => mysite.com [path] => / [site_id] => 1 [siteurl] => https://mysite.com [archived] => 0 [spam] => 0 [deleted] => 0 ) )
Looking at the WordPress MU documentation, I would guess that the
get_blogs_of_user
always returns an array, so checking on the value of$blogs
exists is always going to return true. In the following code, I suggest replacing the simple check on the existence of a value with a check to determine if the returned value is an array and, if so, whether it has elements or not:Try this :
Give this one a shot. Even if it doesn’t work in its current state, it should be easier to see the logic and figure out whats not working properly.
EDIT: Shamelessly stole @JustinPearce‘s method of checking if the user has a blog from his answer