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

Stop the 'rel' field in menu system from filtering characters WordPress

  • SOLVED

I'm looking to see if it's possible to stop the 'Link Relationship (XFN)' field in the WP 3+ menu system from stripping characters.

Specifically, I'm trying to pass a hex color through that field which requires the use of a hashtag at the front of the 6 digit color code. For example, the color '#cccccc' is being outputted as 'cccccc'. The hashtag is required for what I'm trying to do.

Is it possible to change the filter of the 'rel' field so that it allows for the hashtag to go through?

Answers (2)

2011-06-12

Utkarsh Kukreti answers:

WordPress is splitting the xfn value by a space, and then running the `sanitize_html_class` function over it, which strips anything but alphanumeric characters.

wp-includes/nav-menu.php

$args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );


There are no filters around that, so the only way I can think of doing this, if you _really_ want to, is by commenting out this line.

Edit:

sanitize_html_class does have a filter. One way is to not filter any class that starts with a # sign. Let me know if that would work for you.


beowulf comments:

Interesting.

I definitely wouldn't want to make changes to core WP files (it would just be overwritten on the next upgrade anyway).

A function would be ideal although I'm not really sure how to go about that.


Utkarsh Kukreti comments:

add_filter( 'sanitize_html_class', 'dont_filter_hash', 10, 2 );
function dont_filter_hash($sanitized, $raw) {
if(preg_match("/^#[0-9a-fA-F]{6}$/", $raw)) {
return $raw;
} else {
return $sanitized;
}
}


This code will not filter any hex value, with the format "#abcdef".


beowulf comments:

Nice!! That filter totally worked! :)


beowulf comments:

You just made my night with that. Ok, now I need to mark it as answered. Thanks again!

2011-06-12

Caroline Keim answers:

Can you show us the code you're trying to use?


beowulf comments:

I'm not sure how that's relevant. Wordpress is stripping out everything from the 'Link Relationship (XFN)' field other than standard characters regardless of the code I'm using.


Caroline Keim comments:

I was going to say it's relevant, because we could add a script to add the # after Wordpress removed it.


beowulf comments:

Ah, yes. I see your point now. :)

The only thing is that I'm not sure that I want to paste it here. It's a jQuery function that I'd rather keep out of the public domain for now.