Custom URL Override

How to override the default menu item URL, or create a dynamic URL

The URL of a menu item determines where the user will be directed when the item is clicked.

With standard menu items (Post, Page, Term, Archive, etc), this URL is automatically generated by WordPress for the associated content. With Custom Link menu items, you determine the URL by entering it in the “URL” field.

Either way, these links are static – they point to a single URL.

Sometimes, you need to override that standard URL, most commonly if you need the URL to be dynamic.

User Profile Link Example

A common example would be a link that points to a user’s profile. Jim’s profile is at mysite.com/profile/jim , and Mary’s profile link is at mysite.com/profile/mary . You want a link in the menu that says “My Profile” that brings the currently logged in user to their own profile. The URL changes based on the user, so the URL needs to be dynamic.

To accomplish this, you’d need a shortcode which returns the appropriate link. You may have an existing shortcode that does this, or you may need to write your own. So you might create a shortcode called [profile-url]. In your code, you determine the username of the current user and append it to mysite.com/profile. Then you return the generated URL.

You’d place that shortcode in the Custom URL Override field

Then the URL returned by your shortcode will be applied to the link for that menu item.

Please note that the shortcode will be specific to your site, and is not something that UberMenu provides. Writing custom shortcodes is outside the scope of support, but for information on how to write a shortcode, please see How to Create Shortcodes in WordPress. Below is a simple example of creating a shortcode called [profile-url] for generating a custom URL based on your site and the current user’s login name.

add_shortcode( 'profile-url' , 'profile_url' );
function profile_url(){
  $user = wp_get_current_user();
 
  if( !$user->ID ){
    //user not logged in, redirect to login package
    return site_url( 'wp-admin' );
  }
 
  //User is logged in, return dynamic URL
  $username = $user->user_login;
  return site_url( 'profile/' . $username );
}

Note, if you’re using a plugin or theme that generates the URL, they likely have a function you can simply wrap, if they don’t already offer a shortcode.

On this page