phpwordpresssymfonywp-nav-menu-item

Changing URL's in wp_nav_menu Wordpress+Symfony


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.


Solution

  • 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/