Support » Plugins » Hacks » Custom fuction for dynamical links in wordpress menu

  • Hi,

    I hope to get some help because I can not solve this on my own. It´s really frustrating.

    My aim is to create a standard wordpress menu via the build in appearance – menus.
    I want to create a function, that changes an URL like “/authors/–username–/profile.php” to “/authors/max/profile.php”.

    This is my function, added to functions.php

    function filter_function_name( $object = "" ) {
        print_r( $object );
        if ( !exist( $object->url ) ) {
            return $object;
        }
        $user = wp_get_current_user();
        $newlink = str_replace( "--username--", $user->user_login, $object->url );
        $object->url = $newlink;
        return $object;
    }
    add_filter( 'wp_nav_menu_objects', 'filter_function_name' );

    From my opionion, this should work. But when adding this and reload my page, everything disappears. Only the background image (body background) is still there.
    So, I think, there is a mistake somewhere.

    Can anybody find it for me? I have really no idea.

Viewing 6 replies - 1 through 6 (of 6 total)
  • I don’t think ‘url’ is part of the object that gets used here. According to the codex, wp_nav_menu_objects, returns an object “containing wp_nav_menu arguments”. Check the “$args” section on this page to see which arguments are available to you.

    EDIT: After thinking about this a little more, I think this is a better approach:
    You can use wp_nav_menu_items to add an item to the menu and format it however you want. It would look something like this:

    add_filter( 'wp_nav_menu_items', 'login_button_menu_item', 10, 2 );
    function login_button_menu_item ( $items, $args ) {
        $user = wp_get_current_user();
        if ( $args->theme_location == 'MENU THEME LOCATION') {
            $items .= '<li id="nav-login-button"><a href="/blah/'. $user->user_login. '/blah/">Profile</a>';
        }
        return $items;
    }
    Thread Starter digiblogger

    (@digiblogger)

    Hi.

    Thank so much for the time invested here.
    I tried this function before. But it does not work for submenu items. I don’t want to place my menu items in the first level

    Thread Starter digiblogger

    (@digiblogger)

    I want the–username- generally replaced by the current users usernam

    Moderator bcworkz

    (@bcworkz)

    360zen still has the right idea, you just need to adapt it to your situation. I’m assuming you already have a menu item that has an URL containing “–username–“. It will then already be in the $items string passed to the ‘wp_nav_menu_items’ callback. You just need to find that substring in the existing $items and replace it with the current user’s username.

    Thread Starter digiblogger

    (@digiblogger)

    Hi,

    the code above was an idea of a developer. I must say that I don´t really now how to adapt this to my situation.

    Yes you are right, I have menu items with –username– in it.

    Could you be so kind to help me here.

    The developer will not be reachable for nearly 4 weeks. My users are angry about this problem ;(

    Thread Starter digiblogger

    (@digiblogger)

    I am happy cause I found a workaround.

    I just created a shortcode for displaying user_login when entering [userlogin] somewhere.

    add_shortcode( 'userlogin' , 'ss_get_current_userlogin' );
    function ss_get_current_userlogin(){
        $user = wp_get_current_user();
        return $user->user_login;
    }

    Then, I added the possibility of adding shortcodes to the URL:

    add_filter(‘wp_nav_menu’, ‘do_menu_shortcodes’);
    function do_menu_shortcodes( $menu ){
    return do_shortcode( $menu );
    }

    And now, it works great

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom fuction for dynamical links in wordpress menu’ is closed to new replies.