{"id":209676,"date":"2015-10-14T13:54:15","date_gmt":"2015-10-14T20:54:15","guid":{"rendered":"http:\/\/css-tricks.com\/?p=209676"},"modified":"2015-12-08T07:08:37","modified_gmt":"2015-12-08T14:08:37","slug":"the-wordpress-nav-walker-class-a-guided-var_dump","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/the-wordpress-nav-walker-class-a-guided-var_dump\/","title":{"rendered":"The WordPress Nav Walker Class: A Guided var_dump()"},"content":{"rendered":"

The following is a guest post by Scott Fennell<\/a>. Scott is a WordPress theme & plugin developer in Anchorage, Alaska. Here, he customizes the default HTML output of WordPress’ menu system to his liking, without damaging the useful things it already does.<\/em><\/p>\n

There are many things about the WordPress nav menu system that I like, and a handful of things I really dislike. The things I dislike happen to trigger my pet peeves: Markup-bloat and non-SMACCS<\/a>-ish class names. To me, it’s worth a deep dive to correct this situation. However, I don’t want to lose integration with the admin UI, and I don’t want to lose the dynamic class names that core gives us, such as current_page_item<\/code> or current-menu-ancestor<\/code>. Therefore, I’m not going to replace anything: I’m going to extend the PHP class that draws the nav menus: The Walker_Nav_Menu<\/code><\/a> class.<\/p>\n

<\/p>\n

I’ll do this by building a plugin that outputs nav menus with whatever markup and classes I want. Along the way, I’ll stop and smell the roses<\/strike> var_dump()<\/code> the PHP variables that WordPress exposes to us. This plugin will have the following components:<\/p>\n

    \n
  1. A main plugin file to register the plugin and call other files<\/li>\n
  2. A shortcode for outputting the menu<\/li>\n
  3. Some CSS, JS, & SVG to do things like show\/hide submenus<\/li>\n
  4. A custom walker class, which will extend the core Walker_Nav_Menu<\/code> class<\/li>\n<\/ol>\n

    Of those components, all but the last one will act mainly as placeholders. They’ll offer the minimum amount of code in order to achieve the minimum viable product, and I won’t be exploring them in detail. They will provide just enough of a foundation for me to build a custom walker class, which is the focus of this article.<\/p>\n

    Assumptions<\/h3>\n