My Site Address (URL) is http://something.ddns.net/PS. I have a header Custom Links menu URL like this "/#services". This takes me to a location on the current page but I want it to take me to the location on a root page. Is there a variable I can use that represents Site Address (URL) or WordPress Address (URL), e.g. "[site_url]/#services"?

I need this because I want to be able to change my site's URL without going through all the menus and changing the URL. I want menus to reflect the site root from the settings of the Wordpress.

Related question: When I click the link for the first time it does not jump to #services on the page, why do I need to click on it twice in the row?

Edited: Why would anyone want links to be relative to the root and not to the Site Address (URL)?

| improve this question | | | | |

If you have Custom Link URL exactly as written in your example, it should work as expected. "/" at the beginning already means "site root", and it should work on single (first) click. Maybe you have some menu related plugin installed, or your theme changes menu URLs in some way. Or there is client side javascript involved.

What URL do you see in browser status bar (lower left corner) when you hover over custom menu link? It should be [site_root]/#services.


When WordPress is installed in subdirectory /#services will link to incorrect page. There is no way to add [site_root] prefix in menu URLs, so you either use have to use real URL /PS/#services (and will have to change them all after changing site root) or you can add filter to automatically prefix all menu items starting with /. Just keep in mind, that in case you need to add link to real site root, you must use full URL http://something.ddns.net/ instead of /.

function my_nav_menu_link_attributes( $atts ) {
    if ( '/' == substr( $atts['href'], 0, 1 ) ) {
        $atts['href'] = site_url() . $atts['href'];
    return $atts;
add_filter( 'nav_menu_link_attributes', 'my_nav_menu_link_attributes', 20 );
| improve this answer | | | | |
  • My site URL is something.ddns.net/PS, that is why it does not work and why I asked is there a variable that represents my WordPress Address (URL) or Site Address (URL). – Hrvoje Batrnek Jan 31 at 22:28
  • @HrvojeBatrnek see my updated answer. – Vitauts Stočka Feb 1 at 9:17
  • Well, in this function my_nav_menu_link_attributes I could add support for [site_url] placeholder. e.g. [site_url]/#services – Hrvoje Batrnek Feb 2 at 23:06
  • It does not work. Links won't change, It's like the function is not even being called. Why? – Hrvoje Batrnek Feb 8 at 6:32
  • I have other filters and they do work, so I know where to put the code, but this one does not work. – Hrvoje Batrnek Feb 9 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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