Current User In Custom Menu Item URL

I'm using S2Member + Buddypress on a compatible template. I have 8 menu items and am using the "Menu Items Visibility Control" to limit the 7th for view only to current_user_is(s2member_level1) | in_array('administrator', $GLOBALS['current_user']-roles) This visibility is working correctly.

I'm in need of a way to create a custom menu item that will link to a page containing the following structure:

http://localhost/%%current_user_nicename%%/events/my-events/?action=edit

I don't know how to incorporate the current user name into the URL. I've come across this post:.

*There must be an easy fix, if I leave a custom menu item's URL blank..can I specify the url elsewhere in my code?

Topic s2member buddypress menus Wordpress

Category Web


Congrats on figuring it out. Perhaps this would also work:

function change_menu($items){
  foreach($items as $item){

   if ($item-> post_name == 'the-slug')/*replace "the-slug" with menu item post_name */
        $item->url = bp_loggedin_user_domain() . '/events/my-events/?action=edit';

  }
  return $items;

}
add_filter('wp_nav_menu_objects', 'change_menu');

I figured it out with a filter function. This finds the nav menu obj matching the given slug and then modifies it's URL. I left the Menu item with visibility control in my dashboard menu. The URL is initially blank.

Added to functions.php:


function getUN()
{
    global $current_user;
      get_currentuserinfo();

      return  $current_user->user_login;
}

function change_menu($items){
  foreach($items as $item){

if ($item-> post_name == 'the-slug')/*replace "the-slug" with menu item post_name */
{
    $item->url = get_site_url().'/members/'.getUN().'/events/my-events/?action=edit';

  }
  }
  return $items;

}

add_filter('wp_nav_menu_objects', 'change_menu');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.