Answer the question
In order to leave comments, you need to log in
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
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 );
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' );
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' );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question