0

In my symfony project I'm trying to use wordpress for free user-friendly content manager. My problem is, when I'm trying to take Wordpress menu, like:

    $menu = wp_nav_menu( array(
        'menu_id'   => 'top-menu',
    ) );

Ofcourse menu items links to URL's like:

/wordpressfolder/page-item

And the point is: how to change it to:

/somethinganother/page-item

?

I want to do that, because I've changed the standard CMS routing, so I need to be consequent, that the project can look professional.

Ofcourse I'dont mind the js option because it's obvious, but I want to do it on server site if it is possible.

Rallyholic
  • 317
  • 1
  • 3
  • 21
  • Does this answer your question? [How to modify url in wp\_nav\_menu?](https://stackoverflow.com/questions/18785155/how-to-modify-url-in-wp-nav-menu) – Marsellus Sep 08 '21 at 23:30

1 Answers1

4

You could use one of the filters WordPress comes with. The following code example shows a way you could use.

function mmn_main_item_rewrite( $items, $args ) 
{
    foreach ( $items as $item ) {
        $item->url = str_replace( 'wordpressfolder', 'somethinganother', $item->url );
    }

    return $items;
} 
add_filter( 'wp_nav_menu_objects', 'mmn_main_item_rewrite', 10, 2 );

Just add this piece of code in the functions.php file in your active WordPress theme folder. Always use WordPress child themes for individual changes.

More details about the wp_nav_menu_objects hook in the WordPress documentation: https://developer.wordpress.org/reference/hooks/wp_nav_menu_objects/

Marcel
  • 4,854
  • 1
  • 14
  • 24