[Science, Technology, Engineering, Arts and Maths]

WordPress – Add dynamic link to main menu

Let’s say your site has a main menu but you want to add a dynamic link to it. By dynamic link I mean a page that differs from user to user. This is most commonly required when buddypress is installed and you need to add a link to the users profile page. This is not something you can do at the moment from the standard admin interface.

The work around is to use the following code or similar. I needed this to work for a plugin so I added it to my main plugin php file.

/* Add a menu option to link to the logged in members profile */
add_filter('wp_nav_menu_items', 'add_main_menu_profile_link');
function add_main_menu_profile_link($menu) {
// If user not logged in return default menu
if (!is_user_logged_in()) {
return $menu;
} else {
// Add profile link to the end of the menu
$profilelink = '<li><a href="' . bp_loggedin_user_domain('/') . '">Profile</a></li>';
}
$menu = $menu . $profilelink;
return $menu;
}

Leave a Reply

Your email address will not be published. Required fields are marked *