So I'm trying to create a code (in functions.php
) that displays a widget content, from shortcode, based on whether the user is logged in or logged out (visitor). I am successful in displaying content for logged in users, but not for visitors, as I need everything to happen in the same widget. The approach I'm trying is by using two different shortcodes, like this:
[member_only]You are logged in.[/member_only]
[visitor_only]Login / Register[/visitor_only]
If user is logged in, it displays the content of
[member_only]You are logged in.[/member_only]
If user is logged out, it displays the content of
[visitor_only]Login / Register[/visitor_only]
My code:
/* BEGIN LOGIN BUTTON WIDGET SHORTCODE */
function member_only_shortcode($atts, $content = null)
{
if (is_user_logged_in() && !is_null($content) && !is_feed()) {
return $content;
}
}
add_shortcode('member_only', 'member_only_shortcode');
function visitor_only_shortcode($atts, $content = null)
{
if (is_user_logged_in() && !is_null($content) && !is_feed()) {
return "";
}
else {
return $content; }
}
add_shortcode('visitor_only', 'visitor_only_shortcode');
/* END LOGIN BUTTON WIDGET SHORTCODE */