1

I have a shortcode that displays a block of content. Is there a way I can display different HTML depending on if the user is logged in or not? Below is what I have so far.

function footer_shortcode(){
    $siteURL = site_url();
    $logoutURL = wp_logout_url(home_url());

    echo '
        <div class="signin_container"> 
            <h4 class="signin_footer_head">Log In To My Account</h4>
            <a href="'.$siteURL.'/login/">Log In</a>
            <a href="'.$siteURL.'/register/">Sign Up</a>
        </div>
    ';  
}

add_shortcode('footerShortcode', 'footer_shortcode');
1

You can use a conditional tag to check if the user is logged in. Conditionals are very common in W, I would suggest you give this page a read.

Here is an example.

function footer_shortcode(){
    if (is_user_logged_in()){
        echo '
            // Logged In Content
        ';
    }else{
        echo '
            // Logged Out Content
        ';
    }       
}

add_shortcode('footerShortcode', 'footer_shortcode');
1

You are looking for is_user_logged_in()

See: https://developer.wordpress.org/reference/functions/is_user_logged_in/

Example:

function footer_shortcode(){
    $siteURL = site_url();
    $logoutURL = wp_logout_url(home_url());

   if(is_user_logged_in()) { // user is logged in
      echo '
        <div class="signin_container"> 
            <h4 class="signin_footer_head">Account Info</h4>
        </div>
    '; 
   }

   else { //user is not logged in
      echo '
        <div class="signin_container"> 
            <h4 class="signin_footer_head">Log In To My Account</h4>
            <a href="'.$siteURL.'/login/">Log In</a>
            <a href="'.$siteURL.'/register/">Sign Up</a>
        </div>
    '; 
   } 
}

add_shortcode('footerShortcode', 'footer_shortcode');

Edit: I see RiddleMeThis beat me to the punch

  • 1
    Quick Draw for the win : ) – RiddleMeThis Dec 17 '18 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.