Skip to:
Content
Pages
Categories
Search
Top
Bottom

Redirect from page to profile url (menu link)


  • project_subdomain
    Participant

    @project_subdomain

    [WP 4.1.1
    bbp 2.5.6]

    For creating a navigation menu I need to redirect pages to the bbpress profile url as well as to their default options urls. These are domain.com/forums/users/HERE-COMES-THE-USERNAME for showing profile and domain.com/forums/users/HERE-COMES-THE-USERNAME/HERE-COMES-SELECTED-OPTION for selected options.

    I tried several ways with code like this on the page’s template file for the redirection to the profile but nothing worked yet.

    <?php /*  
    Template Name: show user profile
    */ 
    
    $name= bbp_user_profile_url(bbp_get_current_user_id() );
    
    header('Location: http://domain.com/forums/users/'.$name);
    exit();
    
    ?>

    Thanks for helping!

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

  • project_subdomain
    Participant

    @project_subdomain

    just noticed the unlogical doubled url in the code above, but my other tries like for example

    $id = bb_get_user_id();
    $user = bb_get_username();
    $nicename = bb_get_user_nicename('$id','$user');
    
    header('Location: http://domain.com/forums/users/'.$nicename);

    also should have a mistake.


    Robkk
    Moderator

    @robkk

    you can do something like this to a link to your profile in the menu

    add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
    function my_nav_menu_profile_link($menu) {
        if (!is_user_logged_in())
            return $menu;
        else
            $current_user = wp_get_current_user();
            $user=$current_user->user_login ;
            $profilelink = '<li><a href="/forums/users/' . $user . '/">Your Profile</a></li>';
            $menu = $menu . $profilelink;
            return $menu;
     
    }

    project_subdomain
    Participant

    @project_subdomain

    thanks robkk!
    now the profile link is in the main and also in my sidebar menu.
    unfortunately i’d need to have it in the sidebar menu only. also i’d need a link to the single profile pages as I’d like to replace the default display of options below the avatar which I’d remove then.

    which amendments of that code would do that?


    Robkk
    Moderator

    @robkk

    @project_subdomain

    well there is a way for the link to be displayed on a specific menu.

    you could link to site and see if i could find your menus ID to only show it there.

    I’d like to replace the default display of options below the avatar which I’d remove then.

    explain a little bit more on what exactly you want here.


    project_subdomain
    Participant

    @project_subdomain

    @robkk, thanks again for your support. my latest post disappeared, maybe because of too many links.

    regarding the new menu, i hope this image can explain it better:
    the default menu below the avatar shall be removed and appear in the sidebar on the right in a dropdown menu ( not only to make a consistent layout but also to prevent users to see the information also on other user profiles). Optimally it should be shown only when the user is logged in, but yet i’d be very happy to get it working no matter of the user’s login status.

    unfortunately i can’t provide a link at the moment, so i put the id’s at the image.

    parent menu items are “nachrichten” and “account”.
    in “account” the links for the user profiles shall be displayed as sub menus.

    the parent menu-code looks like this:
    <li id="menu-item-566" class="menu-item menu-item-type-post_type menu-item-object-page cur…ancestor menu-item-has-children menu-item-566 dcjq-parent-li">

    the sub menu show profile-code like this:
    <li id="menu-item-572" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-572"></li>

    (for “edit profile” I already found a menu-link solution)
    [btw: default theme twenty twelve]


    Robkk
    Moderator

    @robkk

    my latest post disappeared, maybe because of too many links.

    probably

    the default menu below the avatar shall be removed and appear in the sidebar on the right in a dropdown menu ( not only to make a consistent layout but also to prevent users to see the information also on other user profiles).

    did you create a custom widget?? it might be better if you did for this because then you could output all of the links to the user profile.


    project_subdomain
    Participant

    @project_subdomain

    did you create a custom widget?? it might be better if you did for this because then you could output all of the links to the user profile.

    unfortunately not, as i’m using a plugin (jquery-vertical-accordion-menu) for the accordion/dropdown menu widget for that menu, which makes it more complicate, i guess. does the
    $id_base = 'dc_jqaccordion_widget';
    help with this and if where and what else to put in the code for a custom widget that’s using the accordion/dropdown menu as well?

    initially hoped that making a redirect via permalink in functions.php or via page template would be the fastest and easiest solution. am i totally wrong with this?
    as i’m not familiar with coding, i see a high risk of having set wrong variables all the time. i’m using default bbpress setting with a nicername/username instead of user’s id in the permalink.


    Robkk
    Moderator

    @robkk

    you might have to make a copy of the login widget , and customize it to how you want it.

    Layout and functionality – Examples you can use

    This part of the guide shows you how to add custom links to their profile and also an edit link.

    if you copy the edit link and just change the last slug in the url you could get to most of your profile sections.

    so in the end you might have

    <p><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>edit" >Edit Profile/Change password</a></p>
    <p><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>" >View Profile</a></p>
    <p><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>topics" >Topics Created</a></p>
    <p><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>replies" >Replies Created</a></p>
    <p><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>subscriptions" >Subscriptions</a></p>
    <p><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>favorites" >Favorites</a></p>


    project_subdomain
    Participant

    @project_subdomain

    thanks, great idea! i’ll report as soon as possible (quite busy at the moment, sorry!).


    Robkk
    Moderator

    @robkk

    alright then , i will try to make a copy of the login widget too later and see how it turns out.


    project_subdomain
    Participant

    @project_subdomain

    sorry for the delay! just tried – it works like a charm. Thanks a million!


    project_subdomain
    Participant

    @project_subdomain

    now for the mobile navigation I need to get all profile pages into the main menu, too.

    followed this which also uses the add filter above, but did not get it to work: https://bbpress.org/forums/topic/user-porfile-page/

    1. links don’t work. on profile page no nicename is used. and the created menu links give a 404page as the username is not inserted.
    is: mysite/forums/users/topics
    should: mysite/forums/users/USERNAME/topics

    2. cannot set the location of profile page as submenu item.

    really happy for any help with this!


    Robkk
    Moderator

    @robkk

    now for the mobile navigation I need to get all profile pages into the main menu, too.

    This might be custom development.

    followed this which also uses the add filter above, but did not get it to work: https://bbpress.org/forums/topic/user-porfile-page/

    Does it just not show up?? I am starting to think you have some fancy slideout mobile menu and you want the profile link to show up there?

    1. links don’t work. on profile page no nicename is used. and the created menu links give a 404page as the username is not inserted.

    Does the login widget with profile links not work?? or does the actual links on the profile not work?

    2. cannot set the location of profile page as submenu item.

    This might be a little tough. I think you have to construct a small menu, but it may not work well with your header menu for example. And also if you decide to put just a sub item under something you constructed in the menu section in the backend of WordPress.


    project_subdomain
    Participant

    @project_subdomain

    I’m using the default backend design -> menu for all my menus. Tested this at the main navigation menu on my usual desktop version. Actual links show up but throw a 404-page only when clicking.

    My mobile navigation menu (plugin: Responsive Menu) then simply could use one of those menus.

    Should be also possible to make new menu items via functions to one of the menus created in backend design -> menu, right? Already tried this, but really have no idea if the correct slug ID is correct (mobile-menu is the name I gave the menu in backend):

    add_filter ('wp_nav_menu_items', 'add_account-link');
    function add_account_link ( $items, $args ) {
     if (is_user_logged_in() && $args->menu->slug == 'mobile_menu') {
    
    $items = '<ul><li><a href="#">Account</a></li>';
    
    $items ='<ul><li><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>" >Profile</a></li>';
    $items ='<li><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>replies" >My replies</a></li>';
    $items ='<li><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>topics" >My topics</a></li>';
    $items ='<li><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>favorites" >My favorites</a></li>';
    $items ='<li><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>subscriptions" >My subscriptions</a></li>';
    </ul></ul>;
    }
    return $items;
        }

    Robkk
    Moderator

    @robkk

    Should be also possible to make new menu items via functions to one of the menus created in backend design -> menu, right?

    Maybe? maybe do something similar to what Baw login/logout menu uses, but I haven’t dived deep into this yet.

    The way your code looks is a little odd, you might have to construct it similar to what I posted above.

    This is what I made while messing with it, there could be a different class that your theme uses for sub-menu’s though.

    add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
    function my_nav_menu_profile_link($menu) {
        if (!is_user_logged_in() && $args->menu->slug == 'mobile_menu')
            return $menu;
        else
            $current_user = wp_get_current_user();
            $user = $current_user->user_login ;
            $class = 'sub-menu';
            $profilelink = '
                <li><a href="/forums/users/' . $user . '/">Your Profile</a>
                    <ul class=' . $class . '>
                        <li><a href="/forums/users/' . $user . '/replies">Replies</a></li>
                        <li><a href="/forums/users/' . $user . '/topics">Topics</a></li>
                        <li><a href="/forums/users/' . $user . '/subscriptions">Subscriptions</a></li>
                        <li><a href="/forums/users/' . $user . '/edit">Edit Profile</a></li>
                    </ul>
                </li>
            ';
            $menu = $menu . $profilelink;
            return $menu;
     
    }

    project_subdomain
    Participant

    @project_subdomain

    Thanks so much, robkk!
    I’m using nicename in urls which causes a 404 for users that have a nicename. What could be added to above function to solve that?

    How could I possibly find out the correct menu slug id? Right now the menu appears in every menu (sidebar as well) and regardless of an user’s login status.


    Robkk
    Moderator

    @robkk

    if you need to use the user nicename replace

    $user = $current_user->user_login ;

    with

    $user = $current_user->user_nicename ;

    How could I possibly find out the correct menu slug id? Right now the menu appears in every menu (sidebar as well) and regardless of an user’s login status.

    I guess you can try doing what the top answer said here.

    http://wordpress.stackexchange.com/questions/104301/get-menu-id-using-its-name


    Carp Talk
    Participant

    @gloopy

    Hi guys i have used the code above and it works perfect, one problem i have though is it appears in every menu (3 of them) as shown in the screen shot how do i get it to show only in the top right ?

    Here is the screenshot

    Here is the code i used

    // Filter wp_nav_menu() to profile link
    add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
    function my_nav_menu_profile_link($menu) {
        if (!is_user_logged_in() && $args->menu->slug == 'right-top-nav')
            return $menu;
        else
            $current_user = wp_get_current_user();
            $user = $current_user->user_login ;
            $class = 'sub-menu';
            $profilelink = '
                <li><a href="/forums/users/' . $user . '/">My Account</a>
                    <ul class=' . $class . '>
                        </li><li><a href="/forums/users/' . $user . '/replies">My Replies</a></li>
                        <li><a href="/forums/users/' . $user . '/topics">My Topics</a></li>
                        <li><a href="/forums/users/' . $user . '/subscriptions">My Subscriptions</a></li>
                        <li><a href="/forums/users/' . $user . '/edit">Edit My Profile</a></li>
                    
                
            ';
            $menu = $menu . $profilelink;
            return $menu;
     
    }<code></code>

    Carp Talk
    Participant

    @gloopy

    For anyone looking for a better and easier way to do this, i just added this to header.php after the

    							<?php if ( is_user_logged_in() ) { ?>
                
    <li><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>" >My Account</a>
                    <ul class='sub-menu'>
    <li><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>replies" >My replies</a></li>
    <li><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>topics" >My topics</a></li>
    <li><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>favorites" >My favorites</a></li>
    <li><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>subscriptions" >My subscriptions</a></li>
    <li><a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>edit" >Edit profile</a></li>
                    </ul>
                </li>

    It works a treat and i even guessed the edit lol


    Robkk
    Moderator

    @robkk

    @gloopy

    That works too. 🙂

Viewing 20 replies - 1 through 20 (of 20 total)
  • You must be logged in to reply to this topic.