730wavy
730wavy

Reputation: 704

Multiple IF php with 2 else statements

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

  1. Free user sees a UPGRADE link

  2. Premium Users without site see a REGISTER Link ( the membership array of 2,4 are the levels they have to be either one of )

  3. 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 ) )

Upvotes: 1

Views: 919

Answers (3)

Zach Lysobey
Zach Lysobey

Reputation: 15734

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

<?php
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
// print_r($blogs);
$has_membership_level = pmpro_hasMembershipLevel(array(2,4));
$has_blog = ( current_user_can('edit_posts') && is_array($blogs) && count($blogs) > 0 );

$registerLink = '<a href="/register-site/">Register your Site</a>';
$upgradeLink = '<div>UPGRADE</div>';

function echoBlogLinks($blogs) {
    echo '<ul>';
    foreach ( $blogs as $blog ) {
        if($blog->userblog_id != 1) { 
            echo '<li><a href="http://' . $blog->domain . $blog->path .'wp-admin/">My Site</a></li>';
        }
    }
    echo '</ul>';
}

if ($has_membership_level) {
    if ($has_blog) {
        echoBlogLinks($blogs);
    } else {
        echo $registerLink;
    }
} else {
    echo $upgradeLink;
}

Upvotes: 0

Justin Pearce
Justin Pearce

Reputation: 5097

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:

<?php 
     if (pmpro_hasMembershipLevel(array(2,4))) { 
        if (current_user_can( 'edit_posts' )) :
             global $current_user;
             $blogs = get_blogs_of_user( $current_user->id );
             /*Check if we got an array back and, if so, 
               check if it has elements*/
             if ( is_array($blogs) && ( count($blogs) > 0 ) ) {
                foreach ( $blogs as $blog ) :
                    if($blog->userblog_id != 1) { 
                        echo '<li><a href="http://' . $blog->domain 
                            . $blog->path
                            .'wp-admin/">My Site</a></li>';
                    }
                 endforeach; // end foreach loop
             } else {
                echo '<a href="/register-site/">Register your Site</a>';
             } // end if $blogs
         endif; // endif current_user_can
      } else {   
?>
    <div>UPGRADE</div>
<?php 
     } 
?>

Upvotes: 1

Charaf JRA
Charaf JRA

Reputation: 8334

Try this :

<?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 '<a href="/register-site/">Register your Site</a>'; 
           }
   } 
} else {   ?>
 <div>UPGRADE</div>
<?php } ?>

Upvotes: 0

Related Questions