Conditional shortcodes in page template

Replies
2
Voices
2
Freshness
Followers

1

Hi there,

I’m looking at purchasing this plugin for a website I’m building and I have a question;

I’m building a custom theme for the website and I’m building the page templates myself. I would like to display content conditionally in these page templates depending on whether a user is logged in or not.

 

I tried using the logged in / not logged in shortcodes in my page template but they’re not working as intended; I think because each shortcode has to be used twice (once to open and once to close).

This is my current code:

<?php echo do_shortcode(""); echo do_shortcode("[logged_in]"); ?>
<div class="text"><?php echo $loggedintext; ?> Visit the <a href="<?php echo $forumlink; ?>">Discussion Board</a></div>
<?php echo do_shortcode("[/logged_in]"); ?>

Is there a template tag I can use or another way I can display content conditionally like this?

 

Thanks

 

Hannah

  • Edit

    Hi Hannah,

    If you're going to hard code this in to a template, I would recommend not using the shortcode, instead you can use the is_user_logged_in() function from WordPress. See this page: https://developer.wordpress.org/reference/functions/is_user_logged_in/

    Your code would look something like this:

    <?php if ( is_user_logged_in() ) : ?>
    <div class="text"><?php echo $loggedintext; ?> Visit the <a href="<?php echo $forumlink; ?>">Discussion Board</a></div>
    <?php endif; ?>

    The shortcode should only really be used when using the WordPress post editor.

    Cheers,

    Matt

open

Lucky you! WP Discussion Board has been automatically discounted for a limited time.