• Resolved lucilem

    (@lucilem)


    Hello,

    I try to add the following custom link into the wordpress nav menu but it does not work, how should I do ?

    http://www.mywebsite.com/?page=portrait&id=$mid

    $mid is the id of the currently logged in user.

    Thank you for your help,

    Lucile

Viewing 4 replies - 1 through 4 (of 4 total)
  • The text you put into the menu entries is expected to be HTML and CSS, it will not be examined for PHP variables. What you need to do is to register a filter function, inside the filter function you get to replace a piece of text with the id number.
    List of filters here:
    http://adambrown.info/p/wp_hooks/hook/filters
    It is hard to know just which one to pick, this can require experimentation and reading theme and core code. To my eye’s this one looks like a good candidate: wp_nav_menu
    So I would put this code into my (child) theme’s functions.php file:

    // filter user ID into menu links
    function custom_filter_menu_UserID( $args ) {
    	$id = '';
    	if( is_user_logged_in() ) $id .= wp_get_current_user()->ID;
    	return str_replace('UserId_HERE', $id, $args);
    }
    add_filter( 'wp_nav_menu', 'custom_filter_menu_UserID' );

    And put this into your menu:
    http://www.mywebsite.com/?page=portrait&id=UserId_HERE

    Thread Starter lucilem

    (@lucilem)

    Amazing !! It works perfectly !!
    Thank you very much !!!!

    Glad it worked.
    Just had a thought, what happens if someone just starts to add numbers at random to the url, like this:
    http://www.mywebsite.com/?page=portrait&id=1234
    They get to view any user’s page they have the patience to find.

    If you want people to only view their own pages, then:
    Give the “page=portrait” page a custom page template in your theme, using PHP code in the template set the $id to that of the logged in user, fakery is no longer possible. Of course you also test if the user is logged in, if not display “You must be logged in to view this page”

    Thread Starter lucilem

    (@lucilem)

    Someone pointed that out to me, I thought it’s not that important if users start viewing other members profile that way since there is no such restriction on my website, anyone can view any profile.

    But it’s great that you got a solution for that too ! So I’ll try and implement it as well, thank you very much !!

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

The topic ‘Adding $id variable to custom link in nav menu’ is closed to new replies.