Any way to stop formatting classes for menu items?

Okay so I posted this over on WP.orgs forums and got a typical useless response from a moderator.

Noticed when I save classes in the backend for menu items it strips out some characters, like saving md:flex turns into mdflex after saving it.

Moderator claims it’s a theme related issue, if it is, how do I stop it?

Can confirm this is a wordpress core function.

I bet you could alter it with a filter hook. Here’s some starting points for your search:
wp_update_nav_menu_item()
wp_get_nav_menus()

Sorry, I’m not sure where the sanitization actually happens just yet.

Pretty sure this is happening in the WP Nav Walker. You can see the source here: https://developer.wordpress.org/reference/classes/walker_nav_menu/ I’m on my phone so the code is a little difficult to read, but it looks like the classes are being sanitized by esc_attr(). It looks like all the available filters in the Walker happen before the classes are sanitized, but it looks like there’s a filter on esc_attr that would allow you to filter content after it’s been sanitized, to “re-add” your correct classes: https://developer.wordpress.org/reference/functions/esc_attr/

You could also change your tailwind config to use a separator that WP won’t strip out: https://tailwindcss.com/docs/configuration#separator I’ve done this on a few projects, and it works fine.

This topic was automatically closed after 42 days. New replies are no longer allowed.