[wp-trac] [WordPress Trac] #35127: Allow wp_nav_menu() function to add container attributes, including Schema.org structured data

WordPress Trac noreply at wordpress.org
Wed Mar 9 18:06:00 UTC 2016


#35127: Allow wp_nav_menu() function to add container attributes, including
Schema.org structured data
-------------------------+------------------------------
 Reporter:  sevenspark   |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Menus        |     Version:  4.4
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:
-------------------------+------------------------------

Comment (by yipeecaiey):

 For anyone looking to incorporate schema.org attributes into the nav, you
 can accomplish this using the 'wp_nav_menu' filter.
 Here's a quick function that you can copy and modify as needed:

 {{{
 function filter_wp_nav_menu($nav_menu,$args) {
     global $schemaURL;

     //map out the nav_menu for parsing
     $dom = new DOMDocument();
     @$dom->loadHTML($nav_menu);
     $x = new DOMXPath($dom);

     //parse the <a> nodes
     foreach($x->query("//a") as $node) {
         $node->setAttribute("itemprop","url");
     }

     //parse the <li> nodes
     foreach($x->query("//li") as $node) {
         //$node->setAttribute("itemsomething","xxxx");
     }

     //parse the <ul> nodes
     foreach($x->query("//ul") as $node) {
         //$node->setAttribute("itemsomething","xxxx");
     }

     //parse the <nav> nodes
     foreach($x->query("//nav") as $node) {
         $node->setAttribute("itemscope", "itemscope");
         $node->setAttribute("itemtype",
 $schemaURL.'SiteNavigationElement');
     }

     //regenerate the html
     //NOTE: this assumes only one nav node. Multiple nav nodes will break
 this filter
     $nav_menu = $node->c14n();

     return $nav_menu;

 }
 add_filter( 'wp_nav_menu', 'filter_wp_nav_menu', 10, 2 );
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/35127#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list