0

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 */

1 Answer 1

0

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:

enter image description here

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');
4
  • Ah, I didn't see the site was changing the language in the testing browser, that's because I'm using Polylang plugin. But anyway, can you tell me if I should leave the ` if (is_user_logged_in() && !is_null($content) && !is_feed()) { return ""; }` like that? The return="" part, is there another way of achieving the same result?
    – 023023
    Jul 5, 2020 at 17:32
  • Yeah your logic there is kind of strange, I'll make an example which will simplify it for you
    – mozboz
    Jul 5, 2020 at 18:04
  • Added code example for second shortcode which is a bit cleaner
    – mozboz
    Jul 5, 2020 at 18:08
  • Alright. Thank you very much, everything is way better now.
    – 023023
    Jul 5, 2020 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.