Skip to content

Instantly share code, notes, and snippets.

@jhummel
Created September 15, 2013 22:36
Show Gist options
  • Save jhummel/6574903 to your computer and use it in GitHub Desktop.
Save jhummel/6574903 to your computer and use it in GitHub Desktop.
Add plain text to a Wordpress menu
/* Create the ability to add plain text menu items.
* Add a new Custom Link with your text as the label, and put a "#" in the URL field.
* Add the item, and save your menu. This function will look for any anchor tags
* with a href="#" and strip them out leaving only the text.
*/
add_filter('walker_nav_menu_start_el', 'chlk_make_plain_text_menu_items');
function chlk_make_plain_text_menu_items($items) {
$xml = new DOMDocument();
$xml->loadHTML($items);
$links = $xml->getElementsByTagName('a');
for ($i = $links->length - 1; $i >= 0; $i--) {
$linkNode = $links->item($i);
if($linkNode->getAttribute('href') == '#') {
$lnkText = $linkNode->textContent;
$newTxtNode = $xml->createTextNode($lnkText);
$linkNode->parentNode->replaceChild($newTxtNode, $linkNode);
}
}
$content = preg_replace(array("/^\<\!DOCTYPE.*?<html><body>/si",
"!</body></html>$!si"),
"",
$xml->saveHTML());
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment