I finally succeed in creating the dynamic menu link. I’ve taken a part of code that was made to create an entry in the menu for the latest post and modified it in a way that it obviously works:
// Front end only, don't hack on the settings page
if ( ! is_admin() ) {
// Hook in early to modify the menu
// This is before the CSS "selected" classes are calculated
add_filter( 'wp_get_nav_menu_items', 'replace_placeholder_nav_menu_item_with_mygroups', 10, 3 );
}
// Replaces a custom URL placeholder with the URL to mygroups
function replace_placeholder_nav_menu_item_with_mygroups( $items, $menu, $args ) {
$key = 'http://#mygroups:';
global $current_user;
get_currentuserinfo();
// Loop through the menu items looking for placeholder(s)
foreach ( $items as $item ) {
// Is this the placeholder we're looking for?
if ( 0 === strpos( $item->url, $key ) )
{
// Get the username
$username = $current_user->user_login;
if ( empty( $username ) )
continue;
// Replace the placeholder with the real URL
$item->url = 'http://www.....org/en/members/' . $username . '/groups/my-groups/';
}
}
// Return the modified (or maybe unmodified) menu items array
return $items;
}
If some one with more experience and knowledge sees problems with security or performance I am glad to learn how to change this code.
Try this, untested:
function jan_add_nav_menu_items( $items, $args ) {
if ( is_user_logged_in() )
$items .= '<li><a href="' . bp_loggedin_user_domain() . '/groups/">My Groups</a></li>';
return $items;
}
add_filter( 'wp_nav_menu_items', 'jan_add_nav_menu_items', 10, 2 );
Still works good what I have put together there.
Even if your suggestion looks much easier and more compact, I wonder how to determinate the position in the menu?