Skip to:
Content
Pages
Categories
Search
Top
Bottom

don't want registration page to show after login

  • Avatar of bss
    benjino
    Participant

    @benjino

    Is it possible to have the Registration page or otherwise the Menu item for that page not show on the site after a registered user logs in? So in other words, a registered user sees a different view after loggin in.

Viewing 25 replies - 1 through 25 (of 30 total)
  • Avatar of Hugo
    Hugo
    Moderator

    @hnla

    You would probably need to remove the link from the WP dashboard menu screen as it’s not really possible to apply any logic from that point and instead write a function to place a link to the register page if !is_user_logged_in() and then filtered through ‘wp_nav_menu_items’

    Failing that approach you could take the slightly more cumbersome? approach of simply defining two wp nav menus and display them in their regions based on logged in/out checks.

    Avatar of bss
    benjino
    Participant

    @benjino

    Yes, those sound like good ideas. I’m an experienced enough WP user to see those as possible logical solutions but I am not skilled enough with PHP to know how to do this or to even know what files to add the necessary PHP to.

    Avatar of Hugo
    Hugo
    Moderator

    @hnla

    You’re going to need to get some basic skills especially in the PHP area if working with WP/BP as ther is a need quite often for running up functions – in terms of files to hold that WP provides only one really in themes functions.php, BP provides bp-custom.php that can initiate functions for BP regardless of theme in use.

    For the nav question do a quick search for ‘wp nav menu’ you’ll find previous threads, and also make use of the codex to see if any questions are covered there.

    Avatar of bss
    benjino
    Participant

    @benjino

    I can go to a WP forum for your suggestions if answers for this aren’t provided here. If not here, I do appreciate the concepts you shared, I’ll go with those to WP Forums.

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    You can do this by making use of the wp_nav_menu_items hook to add the register link for logged-out users only. Put this in your functions.php:

    function custom_menu_item ( $items, $args ) {
    
       if ( !is_user_logged_in() && $args->theme_location == 'INSERT-MENU-LOCATION-HERE' ) {
          $items .= '<li><a href="/register" title="Register">Register</a></li>';
       
       } 
       
       return $items;
    }
    add_filter( 'wp_nav_menu_items', 'custom_menu_item', 10, 2 );
    Avatar of bp-help
    bp-help
    Participant

    @bphelp

    @benjino
    This one works for me! Just add it to bp-custom.php or to your themes functions.php between opening and closing php tags.

    
    /* Add registration menu item to logged out visitors only */
    add_filter( 'wp_nav_menu_items', 'bphelp_custom_registration_nav_menu_item' );
    
    function bphelp_custom_registration_nav_menu_item($menu) { 	
    	if ( is_user_logged_in() )
    		return $menu;
    	else
    		$customreglink = '<li><a href="' . get_bloginfo('url') . '/register' . '">' . __('Register') . '</a></li>';
    		$menu = $menu . $customreglink;
    		return $menu;
    }
    
    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    Should have tested my function first as it didn’t work. This one i’ve tested and it does work:

    function custom_menu_item ( $items ) {
    
       if ( !is_user_logged_in() ) {
          $items .= '<li><a href="/register" title="Register">Register</a></li>';
       } 
       
       return $items;
    }
    add_filter( 'wp_nav_menu_items', 'custom_menu_item' );
    Avatar of Hugo
    Hugo
    Moderator

    @hnla

    Guys this has been covered! which is why I requested a site search, not to be mean but to follow a forum principle of not repeating itself and of using it’s archives. :)

    the latter functions miss an important trick covered in a previous post on this by mercime and in the first example here by Henry and will be, in all likelihood, spitting out the link on all menus, possibly the desired effect?

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    @hnla Yes, spot on. The first function i wrote in a rush, i tired to target a specific menu but when i tried testing the function it didn’t work as expected. Although my second function will work, you’re right it will spit out the register link to all WP menus that are in operation

    Avatar of bss
    benjino
    Participant

    @benjino

    Hugo, I didn’t start here, I did the search for ‘wp nav menu’ found Henry’s post at the top of that search and clicked on it’s title and it brings me here, so I hope I’m not upsetting you by continuing here.

    Henry, I used that code, it works, thanks. However, it places the menu item at the far right of the menu and it is desired somewhere in between menu items, and also has a sub-menu item with it, and now the sub-menu item isn’t there of course. How would have the Register menu item come in where I want it to and retain the sub-menu?

    Avatar of Hugo
    Hugo
    Moderator

    @hnla

    @benjino before Henry or bp-help posted if you had searched and scrolled down you would have seen the post to which I referred, but no problem :).

    Avatar of bss
    benjino
    Participant

    @benjino

    Henry, how would you target or filter out other menus. I have a Main menu and a footer menu. Yes, I see it is occurring on both menus.

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    @benjino the whole menu is filtered so you can build as much of it or as little of it as desired.

    Take the portion of my code that builds the link:

    <li><a href="/register" title="Register">Register</a></li>

    Changing this to

    
    <li><a href="/link-before" title="Link before">Link before</a></li>
    <li>
    <a href="/register" title="Register">Register</a>
    <ul>
        <li><a href="/sub-item" title="Sub item">Sub item</a></li>
       </ul>
    </li>
    <li><a href="/link-after" title="Link after">Link after</a></li>
    

    will allow you to output a highly customised menu.

    As @hnla said, if you’re using my second function, then this will be applied to all WP menus you have set up.

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    OK here is my revised first function (which works now). It will target a specific menu of your choosing. Just replace CHANGE-THIS with the name of the menu you want to target.

    function custom_menu_item ( $items, $args ) {
    
       if ( !is_user_logged_in() && $args->menu == 'CHANGE-THIS' ) {
          $items .= '<li><a href="/register" title="Register">Register</a></li>';
       
       } 
       
       return $items;
    }
    add_filter( 'wp_nav_menu_items', 'custom_menu_item', 10, 2 );
    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    @hnla I searched for the thread you mentioned which had the solution already posted. Can be found here:

    http://buddypress.org/support/topic/adding-dynamic-profile-link-to-main-menu-item/

    Avatar of bss
    benjino
    Participant

    @benjino

    Ok, I can understand what you are showing me customizing the menu, but I don’t get how to change the PHP function to insert into only the #primary-nav, and not both #primary-nav and #footer-nav.

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    @benjino use the last function i posted, making sure to replace CHANGE-THIS with the name of the menu you want to target. The name of the menu can be found in WP Admin, when you set-up the menu, you had to give it a name.

    Avatar of bss
    benjino
    Participant

    @benjino

    In the Menu Admin area of WP my main menu is named Main-Menu and I replaced CHANGE-THIS with that menu name, and updated the link and page name too and it didn’t work. It doesn’t insert the the Registration menu link in the Main-Menu. Here’s what I have:

    function custom_menu_item ( $items, $args ) {
    
       if ( !is_user_logged_in() && $args->menu == 'primary-nav' ) {
          $items .= '<li><a href="/register-2" title="Signup">Signup</a></li>';
       
       } 
       
       return $items;
    }
    add_filter( 'wp_nav_menu_items', 'custom_menu_item', 10, 2 );
    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    @hnla actually suggested an easier option i almost overlooked. You could set up 2 menus in WP Admin and display them in your theme according to the logged-in status of the user:

    <?php
    if ( is_user_logged_in() ) {
         wp_nav_menu( array( 'theme_location' => 'user-loggedin-menu' ) );
    } else {
         wp_nav_menu( array( 'theme_location' => 'user-loggedout-menu' ) );
    }
    ?>
    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    With reference to your code, you have primary-nav where you should have Main-Menu. Also, don’t forget to log out when you check if the register link is in the menu. It’ll only be visible to logged-out users

    Avatar of bss
    benjino
    Participant

    @benjino

    Oh yah, I do, I tried it with Main-Menu too and it didin’t work.

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    It works for me on a menu I’ve called footer.

    I reckon the 2 menu approach will be easier anyway . you’ll have to create another menu in wp admin but that should be straight forward just a little time consuming.

    Avatar of bss
    benjino
    Participant

    @benjino

    I created a new menu in wp admin and tried the 2-menu method, and it inserted a completely unstyled menu above the header of the site pushing everything down, and when I logged out things got even worse, all this error code showed up along with the unstyled menu. This is what I put in the functions.php file:

    if ( is_user_logged_in() ) {
         wp_nav_menu( array( 'theme_location' => 'Main-Menu-logged-in' ) );
    } else {
         wp_nav_menu( array( 'theme_location' => 'Main-Menu' ) );
    }
    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    That code needs to go in your theme, replacing your current menu. It doesn’t go in functions.php. Try looking in header.php. you’re looking for wp_nav_menu

    Avatar of bp-help
    bp-help
    Participant

    @bphelp

Viewing 25 replies - 1 through 25 (of 30 total)

You must be logged in to reply to this topic.