The code exactly as you put it in your question works fine for me and does exactly what you suggested.
To make it work I had to make sure my WP post editor was in code editor mode as HTML or Visual Editor may create extra problems:
And this is what my test code in the post editor looked like, when I had a successful test:
<!-- wp:html -->
<p>this is the content</p>
<p>more content
[member_only]You are logged in.[/member_only]
[visitor_only]Login / Register[/visitor_only]
</p>
<!-- /wp:html -->
You can make the second shortcode a bit clearer to read by adding ! for ‘not’ on the front of the first condition, then the structure is the same as the first shortcode, and you don’t need the else:
function visitor_only_shortcode($atts, $content = null)
{
if (!is_user_logged_in() && !is_null($content) && !is_feed()) {
return $content;
}
}
add_shortcode('visitor_only', 'visitor_only_shortcode');
