• Hi,

    Does anyone know why WordPress strips the HTML tags from the menu item descriptions?

    I know how to overcome this with a couple of filter functions written in the child theme’s functions.php, however I have not been able to establish the actual reasoning behind the decision to strip tags in the first place..

    I usually assume there is a good reason for excluding what seems to be a very useful and intuitive option from the core functions, and prefer not to interfere with things that I don’t know enough about until I feel comfortable that there is no overriding factor in terms of security, or other negative side effects.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Matt Knowles

    (@aestheticdesign)

    Are you referring to the Title Attribute?

    Are you using a WordPress Custom Menu or the standard one that WordPress generates?

    I just checked on one of my sites and the Title Attributes display in the menu.

    When asking questions in the WordPress forums, it’s always a good idea to include the URL to your site, and what theme you’re using, as well as any relevant plugins you’re using.

    It might be that your theme isn’t displaying them, but the standard WordPress themes seem to.

    Thread Starter gecko_guy

    (@gecko_guy)

    Hey there,

    Thanks for the interest and taking time to respond.

    Are you referring to the Title Attribute?

    No, I am referring to the Description Field. It is usually not enabled by default, and needs to be activated by opening the Screen Options tab in the Menus settings page.

    You tick the option under: Show advanced menu properties –> Description

    There is no url currently. The behaviour I’m speaking about is part of the WordPress core.

    The Description Field strips out any html tags one places in there, by design.

    The way to override this behaviour is by using some filters, like this:

    //First remove the default WordPress behaviour
    remove_filter( 'nav_menu_description', 'strip_tags' );
    
    //Now Allow HTML in the WP Menu Item Descriptions
    add_filter( 'wp_setup_nav_menu_item', 'wrfc_wp_setup_nav_menu_item' );
        function wrfc_wp_setup_nav_menu_item( $menuItem ) {
          if ( isset( $menuItem->post_type ) && 'nav_menu_item' == $menuItem->post_type ) {
            $menuItem->description = apply_filters( 'nav_menu_description', $menuItem->post_content );
        }
          return $menuItem;
    }

    However, HOW to do it is not the question. I am hoping for someone with advanced insight into php, or who has advanced knowledge of WordPress, and the WordPress Menu API, to be able to offer further detail behind the reasoning of WHY html tags are stripped from the description field by default.

    In my experience the WP developers tend to have firm reasons for doing things in a certain way, so I prefer not to blindly alter the default filters and settings until I understand why they have been built in the way they have first.

    Matt Knowles

    (@aestheticdesign)

    Learned something new. I’ve never seen a site using the descriptions and didn’t know they existed since the option is normally hidden.

    If you feel the behavior of WordPress should change, you might post your request here:

    https://www.ads-software.com/support/forum/requests-and-feedback

    You could also add a ticket for an enhancement directly to the menu team here:

    https://make.www.ads-software.com/core/components/themes/menus/

    Thread Starter gecko_guy

    (@gecko_guy)

    Hey there, I’m not actually suggesting anything should change, nor do I actually need it to in 99.9% of all the sites I build. I have already changed the behaviour to suit my own needs in the few cases where I would like html in the descriptions..

    Rather, I am simply casting a line out there to see if anyone knows the reason for this behaviour? Why has it been designed this way?

    It is a very deliberate choice by the developers, as seen via the inclusion of the ‘strip_tags’ filter…

    i.e. Does it carry any kind of security, performance, or other kind of unwanted risk or side-effect.

    Good idea to contact the menu dev team directly though, thanks for the suggestion.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Why does WP strip menu description html tags?’ is closed to new replies.