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