Created
September 15, 2013 22:36
-
-
Save jhummel/6574903 to your computer and use it in GitHub Desktop.
Add plain text to a Wordpress menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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