Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

wp_nav_menu filter - custom entries stripping { } characters WordPress


Hello WP Questions,

I have run into an issue where I need a filter of some sort for the wp_nav_menu function. I am using a lot of custom entries for the nav menu for a very customized theme.

Basically, every category is a section of site and when you are in that section of site the menu changes. Each menu has a dynamic custom menu that performs a search based on the category.

The problem, adding a custom menu entry URL strips characters like {}. I have a variable I need to pass to the site which is working everywhere in the theme {$refid} but doesn't work in the wp_nav_menu because when I save the entry the curly brackets are stripped.

I assume a filter that changes the arguments for nav menu output would be sufficient but I don't know. But I need to be able to use the wp_nav_menu functionality and apply root relative links that can pass this variable through the menu.


Answers (2)


Julio Potier answers:


i just tried this and works :

function gary_url( $url, $original_url )
return esc_html( $original_url );
add_filter( 'clean_url', 'gary_url', 10, 2 );

See you soon !

edit : put this code into your functions.php theme file.

Gary Smith comments:

It's simple, it works, carries the variable and no hassle. I have 1 question... what does the 10, 2 have to do with the filter?

Julio Potier comments:

10 = priority (default is 10)
and i need to "catch" the $original_url variable, the 2nd one. So i have to pass "10" to gain acces to the 2nd var i need.
Then i added "2", the "apply_filter" from WP Core will pass me "2" vars, $url and $original_url to my filter.
So i can "play" with $original_url, escape it, and return it for you !

Gary Smith comments:

Simple enough... it worked without a hassle and I've already fixed the site menus.


case closed for sure.

Julio Potier comments:

Glad to help !

My left eye is reading the WP Core while my right eye checks that I do not pee on the side ;)


John Cotton answers:

There is a filter called wp_nav_menu_objects which might be useful.

As you can see with the code below, you can change values or remove them all together.

function my_nav_menu_objects( $items, $args ) {

$items[0]->title = 'Change The Title';
$items[0]->url = '';
return $items;
add_filter( 'wp_nav_menu_objects', 'my_nav_menu_objects', 0, 2 );

You can also add to the array so perhaps instead of having a menu item added through the dashboard you could add dynamically in this function using the various globals to determine what's about to be on screen and thus changing your input.