$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>conditional tags - Display Footer Shortcode Different for Logged In Users|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

conditional tags - Display Footer Shortcode Different for Logged In Users

matteradmin9PV0评论

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');

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');
Share Improve this question edited Dec 17, 2018 at 16:19 RiddleMeThis 3,8078 gold badges22 silver badges30 bronze badges asked Dec 17, 2018 at 16:10 JonJon 3275 silver badges17 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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');

You are looking for is_user_logged_in()

See: https://developer.wordpress/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

Post a comment

comment list (0)

  1. No comments so far