A
A
Alexander Miranovich2020-05-20 18:17:25
WordPress
Alexander Miranovich, 2020-05-20 18:17:25

How to remove domain in url menu in wordpress?

Not familiar with wordpress, wp_nav_menu should return links without a domain, i.e. not http://name-site/, but just/

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WP Panda, 2020-05-20
@aleks_664

Hang yourself here
https://github.com/WordPress/WordPress/blob/001ffe...

function wpp_remove_domain_in_nav_menus( $item_output, $item, $depth, $args ) {
    return str_replace( get_home_url(), '', $item_output );
  }

  add_filter( 'walker_nav_menu_start_el', 'wpp_remove_domain_in_nav_menus', 10, 4 );

I
Igor Vorotnev, 2020-05-21
@HeadOnFire

In general, there are several ways, and they all solve the problem - but with varying degrees of impact on performance and possible side effects (or vice versa - their absence). Personally, I prefer to write any code as "close to the body" as possible so that changes occur with a minimum amount of body movements.
For example, the variant from WP_Panda is absolutely working. But the use bothers me get_home_url()- and there is no point in it, if you do not have a multisite (which is most often), it is enough to use get_option( 'home' ). The effect is the same, CPU cycles used - less.
Further, I don’t really like iterating over long lines that could potentially break entirely, it’s more comfortable to work with data before it is assembled into HTML:

function absolute_to_relative_url( $atts )
{
    $atts['href'] = str_replace( get_option( 'home' ), '', $atts['href'] );

    return $atts;
}
add_filter( 'nav_menu_link_attributes', 'absolute_to_relative_url' );

But here, too, my inner nerd will still not be satisfied, because get_option( 'home' )(or get_home_url()) is constantly called in our place, although this value does not change. Plus, our logic is executed on a filter that runs for each menu item - these are all extra CPU cycles too. So I would do like this:
function absolute_to_relative_url( $sorted_menu_items )
{
    $host = get_option( 'home' );

    foreach ( $sorted_menu_items as $item ) {
        $item->url = str_replace( $host, '', $item->url );
    }

    return $sorted_menu_items;
}
add_filter( 'wp_nav_menu_objects', 'absolute_to_relative_url' );

Benefits - our callback is called only once for one menu, the site address is obtained once, a simple loop quickly iterates over menu item objects (data).
You don't have to do this, you can just filter the HTML - and it will work, and there will be no noticeable performance difference if it is done only in this one place. But your approach to writing code in general is important. If approached without being nerdy, then performance problems will begin to appear in large and complex projects. You can solve this problem immediately "more optimally", the time and number of lines are the same, the effect is the same. But in the long run - both for a specific project and for your career as a whole, it is more useful if you solve problems in the most effective way.
PS: Nerds will understand, lazy people will not :)

Z
zorca, 2020-05-20
@zorca

5ec54f51c6ce8405024235.png
If it is necessary to delete the domain from ALL menu links, then make your own Navwalker.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question