WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Part of my work is to create Wordpress websites. I usually work on my laptop until I have something good enough to be uploaded to the test server where the client reviews it.

I create a VirtualHost for every new project so I'm always working with a Wordpress installation in a domain that looks like http://local.example.com/, but when the site is uploaded to the test server (not controlled by me), the domain may end being something like http://testserver.com/arbitrary/path/example/.

The problem is that if I add a custom link to a menu that points to, for example, /events/, it would work fine locally creating a link to http://local.example.com/events/, but in the test server the link will point to http://testserver/events/, which is obviously not right.
What I want is to give the custom link an URL that would work both on my local environment and the test server.

I already handle the problem of changing the home and siteurl Wordpress options by:

  • changing those settings on the local database
  • creating a dump of the database
  • update the database on the server
  • restoring the local options.

I don't want to use full URLs for the custom links and having to replace those with the server URL every time I need to update the server's database.

For links inside the post content there is a plugin that solve the problem adding two shortcodes: http://wordpress.org/extend/plugins/url-shortcodes/, but I haven't been able to find something similar for Custom Links.

share|improve this question
Willington, i cant help you to an url that works anywhere. What i can do is point you to spectacu.la/search-and-replace-for-wordpress-databases. I've been using David's search&replace for quite some time and i works good to change e.g. url's - even when in serialized data. So that is how i do it: just hardcoded links and convert after moving the database to another domain. Good luck, Peter – Peter Mar 11 '11 at 21:30
So far the only way that I've found to successfully move from a dev environment to a live environment is after doing the SQL dump to do a search and replace on the entire file, not just change the home and siteurl options. I've had files where the URL was in there over 1000 times. (still don't know how it managed to get that high :) ) – Rob Williams Mar 13 '11 at 9:26
@Peter, @Rob thank you guys for answering, I was afraid that search-and-replace was the only solution but wanted to ask first. I'll take a look at that script. – wvega Mar 13 '11 at 22:34
Best is to use a script like the one I suggested. Notice that a text search & replace in a db dump will screw up serialized data (especially found in wp_options) if and when the length of the search and replace strings is not the same, since that data is stored in the db with a length specification. Success, Peter – Peter Mar 14 '11 at 8:42

First you have to install this plugin for URL shortcodes.

Add this code to your functions.php file in your theme:

class description_walker extends Walker_Nav_Menu {
    function start_el( &$output, $item, $depth, $args ) {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

        $class_names = $value = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
        $class_names = ' class="'. esc_attr( $class_names ) . '"';

        $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) . '"' : '';
        $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) . '"' : '';
        $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) . '"' : '';

        // echo $item->url;
        $string = explode( '::', $item->url, 3 );
        if ( $string[1] ) {
            $string[1] = str_replace( '-', ' ', $string[1] );
            $item->url = do_shortcode( "[$string[1]]" ); 

        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

        $prepend = '<strong>';
        $append = '</strong>';
        $description  = ! empty( $item->description ) ? '<span>' . esc_attr( $item->description ) . '</span>' : '';

        if ( $depth != 0 ) {
            $description = $append = $prepend = "";

        $item_output  = $args->before;
        $item_output .= '<a'. $attributes . '>';
        $item_output .= $args->link_before . $prepend . apply_filters( 'the_title', $item->title, $item->ID ) . $append;
        $item_output .= $description . $args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );

Then you have to call the wp_nav_menu function from the templates files:

$arg = array( 
    'menu'        => "main-menu", 
    'echo'        => true, 
    'fallback_cb' => 'wp_page_menu', 
    'depth'       => 0, 
    'walker'      => new description_walker() 
wp_nav_menu( $arg );

That's it. Then go to the back-end menu section.

For example, if I want to give the page URL to a custom link, I will add it like this:


Now you can go to the front-end and check that the shortcode works.

share|improve this answer

On a custom URL in the Menu setup it is possible to use relative links to the [blogurl]. The secret is to start the relative URL with a single /. When a single / starts the custom URL the system will not prepend the typical http:// and then the current blogURL will be generated in the target URL at execution time.

If you want to go to your home page, simply put / as the custom URL

If you want to go to the index page in the folder bbforums then put /bbforums as the custom URL.

This allows you to move a site to a test domain without having to hard code the new blogURL in all the custom links for the menus.

For Example:
If my blog is http://example.com and I want to test it in a subdomain http://test.example.com the site can be moved between test and production without menu problems using the relative URL convention noted above. I have successfully tested this approach using the XCloner plugin to move the site.

share|improve this answer
This doesn't work for blog URLs that have slashes, for example example.com/blog. Following the menu item '/' would just take you to example.com. – Jimpanzee Oct 3 '14 at 11:11

Thats great but that does not support the actually nice menu design in WP or example if one changes a article or post name, wp would update the url in your menu´s

using custom links does not support that so that solves your problem only to create another

if your clients adds a page and expected that to appear in his menu then its useless or if they remove a page, rewrite and then want to add to wp again

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.