Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Can someone tell me the if tags for not logged in users please?


  • Tom
    Participant

    @jeffreeeeey

    I’m trying to add a button in the main nav menu (Customized theme), saying ‘Register’.

    But obviously I only want it to appear when users are not logged in. When they are logged in, it should not display nothing.

    Here’s what I’ve been trying with:

    <li<?php if ( !is_user_logged_in()?>><a href="register">Register</a ?>>

    (If FAR from being a coder, so please, dont laugh :D)

    I’ve been trying to work this out (after searching the forums) for hours, and now finally gave in and thought it were time to ask here. Hope someone can help me… thanks

Viewing 17 replies - 1 through 17 (of 17 total)

  • Tom
    Participant

    @jeffreeeeey

    And I forgot to say..

    when you are on that particular page, the nav menu uses the diffent css classes…

    ‘class=”selected”‘

    like so…

    #header ul li.selected a {

    …etc. So if possible, can you tell me how to add that to my “register” link also. thank you.


    Tom
    Participant

    @jeffreeeeey

    HELP pleaeeeease?


    jamesyeah
    Participant

    @jamesyeah

    To do this kind of thing I use the ‘widget logic’ plugin, you can then add a login/register link in the sidebar and make it only visible to non logged in users with a code like this, theres loads of examples in the widget FAQ:

    !is_user_logged_in()

    hope that’s useful :-)


    Boone Gorges
    Keymaster

    @boonebgorges

    You’ve got the right idea but your syntax is wrong. Presumably you don’t want the button (ie the li list item) to show up at all for logged-in users. So you need to put the entire button inside of an if statement:

    <?php if ( !is_user_logged_in() ) { ?><li><a href="register">Register</a></li><?php } ?>

    Also, be patient! 9.5 hours to wait for help isn’t all that much time! :)


    Tom
    Participant

    @jeffreeeeey

    Thank you both.

    Boone- sorry for being what occurs to be impatient; I’m new to this so not sure what the support is like so far.

    Is anyone able to tell me how I’d add an “else” tag to something like this?

    For example, if I want “My profile” to show to logged in users?

    Thanks again.. much appreciated.


    jivany
    Participant

    @jivany

    Hi, please don’t take this the wrong way but…

    There are an endless number of sites online dedicated to learning PHP. What you’re having trouble with is relatively basic PHP. Once you have an understanding of PHP then making the changes you desire will be much easier for you.


    Tom
    Participant

    @jeffreeeeey

    Yes, but the only php I have experience with is that of which vBulletin is coded with. The dependancies for vBulletin are different from WP/ Buddypress, hence why I need a little help finding out the basics. Once I know a little to get me going, I’m sure ‘ll pick it up a lot easier.

    I assure you that before even creating this thread, I done an awful lot of research into it. After hours of getting nowhere, I came to what I thought was the correct place to ask such question.

    Seems a shame that after only just being introduced to WP/ Buddypress, and having only created one support thread, I’m already being advised to look elsewhere.


    Tom
    Participant

    @jeffreeeeey

    To add; should anyone else be looking to do what I am (without the else tag), here’s how to do with so that when the user is on the REGISTER page, the tab is highlighted:

    <?php if ( !is_user_logged_in() ) { ?><li<?php if ( bp_is_page( BP_REGISTER_SLUG ) ) : ?> class="selected"<?php endif; ?>><a href="register">Sign up!</a><?php } ?>


    Boone Gorges
    Keymaster

    @boonebgorges

    Glad you got the extra bit, jeffreeeeey, but you might want to clean up that code a bit, to make sure you close the HTML tags correctly:

    <?php if ( !is_user_logged_in() ) { ?><li <?php if ( bp_is_page( BP_REGISTER_SLUG ) ) : ?> class="selected"<?php endif; ?>><a href="register">Sign up!</a></li><?php } ?>

    What exactly do you want the else clause to do? Do you want different button to show up when the user is logged in?


    Tom
    Participant

    @jeffreeeeey

    Thanks for that Boone.

    Yes basically I want to show a different link if the member is logged in. (My profile)

    I searched & searched for a list of dependencies but can’t seem to find them.


    Tom
    Participant

    @jeffreeeeey

    The website concept I’m working on is a competition. Anyone can register on the site, but upon registration, they’ll have to choose from the following:

    Enter competition

    Don’t enter competition

    This brings me onto another question which is related to this topic. I’d like to have another link in that top nav menu-

    Entrants

    And as a sub-menu to this nav item-

    Men

    Women

    When you click entrants; you’d be taken to a page showing both men AND women who chose the profile field “Enter the competition”.

    And if you click “Men” or “Women” it will display only those of that sex, that have chosen “Enter the competition”.

    Is this something that’s achievable?


    jivany
    Participant

    @jivany

    @jeffreeeeey: Sorry that you took offense that you didn’t get instantaneous help about a PHP coding issue from a BP community supported forum.


    Tom
    Participant

    @jeffreeeeey

    I never took offense- don’t worry.


    Boone Gorges
    Keymaster

    @boonebgorges

    This is getting a bit complicated, and will involve calling the $bp global variable, so it’s best to move it to a self-contained function that will then be called in your templates.

    Put the following in your child theme’s functions.php file (create one if it doesn’t exist, making sure you have the opening and closing <?php and ?> tags).

    function jeff_signup_profile_link() {
    global $bp;

    if (!is_user_logged_in() ) {
    echo '<li ';
    if ( bp_is_page( BP_REGISTER_SLUG ) )
    echo 'class="selected"';
    echo '><a href="register">Sign up!</a></li>';
    } else {
    echo '<li ';
    if ( $bp->loggedin_user->domain == $bp->displayed_user->domain )
    echo 'class="selected"';
    echo '><a href="';
    echo $bp->loggedin_user->domain;
    echo '">My Profile</a></li>';
    }
    }

    Then, in your template file where you’d like the link to appear, use

    <?php jeff_signup_profile_link() ?>


    Boone Gorges
    Keymaster

    @boonebgorges

    As for your other question, here are a few places to start:

    1) Make a Gender field and a Enter the Competition field in Dashboard > BuddyPress > Profile Fields. Make sure they’re required on registration.

    2) If by “top nav menu” you mean the thing that floats across the top of the screen, you can add to it by creating a button inside of a php function, then using add_action to attach the function to the bp_adminbar_menus hook. See buddypress/bp-core/bp-core-adminbar.php to see how it works in the core code.

    3) Use a plugin like JJJ’s https://wordpress.org/extend/plugins/bp-member-filter/ to create the filters that will populate the pages that the “Men” and “Women” dropdowns will point to.


    Tom
    Participant

    @jeffreeeeey

    Boone; I can’t thank you enough. I’ll get let you know how I get on with this- seems my night is now planned :)


    Boone Gorges
    Keymaster

    @boonebgorges

    My pleasure. Good luck getting acquainted with BuddyPress :)

Viewing 17 replies - 1 through 17 (of 17 total)
  • The topic ‘[Resolved] Can someone tell me the if tags for not logged in users please?’ is closed to new replies.
Skip to toolbar