Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

3 Answers

I have tried and got the result for it. Here i explained how i did it.

First you have to install the this plugin for url short-code -> http://wordpress.org/extend/plugins/peters-blog-url-shortcodes/

Add this code to your functions.php file with 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);
            $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 this function from wp_nav_menu 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 );

Thats it. Then go to the back-end with menu section.

For example i want to give the page URL to custom link.

I will add like this -----> http://::blogurl-id='1302'::

That's All.

Now you can go to the front-end and check the shortcode will work.


share|improve this answer
add comment

check this out;

will loop through the links in your menu, fig­ure out which ones don’t begin with a “/” or “http” and prepend them with your site’s URL and a “/”

share|improve this answer
You could also try to enter your url as "/relative_url" and it'll go to domain.com/relative_url>;.. (from regneel) –  numediaweb Apr 4 '12 at 15:11
that ain't a fully relative URL. That's a root-relative URL, and that won't work in the OP's case since his dev site is not on his dev server's root. –  Tom Auger Oct 10 '12 at 3:49
add comment

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
add comment

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.