Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
add dynamic url to menu item to wp nav menu with replacement strings
/*--------------------------------------------------------------
# add dynamic profile link to main menu. smart and simple! just do a string replace on a menu link item
# add a link item and set the url to --url-- and give it a css class of iscurrent
--------------------------------------------------------------*/
function wp_dynamic_profile_link( $menu ){
//$link may have to be changed to your current users profile or author page or whatever...
//i had a post type profile link to each user
$link = get_author_posts_url( get_current_user_id() );
//link menu item appends http:// for us so we need to take it off ours.
$link = str_replace('http://', '', get_permalink($link));
//make menu current-item if it is current page
$actual_link = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if($actual_link == $link){
$menu = str_replace('iscurrent', 'current-menu-item', $menu);
}
return $menu = str_replace('--url--', $link, $menu );
}
add_filter( 'wp_nav_menu', 'wp_dynamic_profile_link' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.