Skip to content

Instantly share code, notes, and snippets.

@saltnpixels
Last active April 14, 2016 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saltnpixels/5375ea9d629c0737a52e1ad4f834da97 to your computer and use it in GitHub Desktop.
Save saltnpixels/5375ea9d629c0737a52e1ad4f834da97 to your computer and use it in GitHub Desktop.
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