Skip to content

Instantly share code, notes, and snippets.

@cahnory
Created February 18, 2016 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cahnory/27f5a2cb0115733cc15c to your computer and use it in GitHub Desktop.
Save cahnory/27f5a2cb0115733cc15c to your computer and use it in GitHub Desktop.
Wordpress nav menu object
function get_menu_tree($args) {
$walker = new Walker_Nav_Menu_Object();
wp_nav_menu(array_merge($args, array(
'walker' => $walker,
'echo' => FALSE
)));
return $walker->getOutput();
}
var_dump(get_menu_tree(array('menu' => 'header')));
<?php
class Walker_Nav_Menu_Object extends Walker_Nav_Menu
{
protected $scopes = array();
public function __construct() {
$this->scopes[] = (object)array(
'children' => array(),
'parent' => NULL
);
}
public function getOutput() {
return sizeof($this->scopes) ? $this->scopes[0] : array();
}
// Output scope
public function &getScope() {
return $this->scopes[sizeof($this->scopes) - 1];
}
public function unscopeEl() {
array_pop($this->scopes);
}
public function scopeEl($el) {
$this->scopes[] = $el;
}
// Walker pattern
public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
$args = apply_filters('nav_menu_item_args', $args, $item, $depth );
$scope = &$this->getScope();
$child = (object)array(
'id' => $id,
'item' => $item,
'children' => array(),
'parent' => &$scope,
'attr' => $this->getElAttributes($item, $depth, $args),
'title' => $this->getElTitle($item, $depth, $args)
);
// add child to scoped el
$scope->children[] = $child;
// set child as new scope
$this->scopeEl($child);
}
public function end_el(&$output, $depth = 0, $args = array()) {
// unscope ended child
$this->unscopeEl();
}
public function start_lvl(&$output, $item, $depth = 0, $args = array()) {
// nothing to do here
}
public function end_lvl(&$output, $depth = 0, $args = array()) {
// nothing to do here
}
// Element HTML attributes
public function getElTitle($item, $depth, $args) {
$title = apply_filters('the_title', $item->title, $item->ID);
$title = apply_filters('nav_menu_item_title', $title, $item, $args, $depth);
return $title;
}
public function getElAttributes($item, $depth, $args) {
$attr = array(
'id' => $this->getElIdAttribute($item, $depth, $args),
'class' => $this->getElClassAttribute($item, $depth, $args),
'title' => $this->getElAttribute($item, 'attr_title'),
'target' => $this->getElAttribute($item, 'target'),
'rel' => $this->getElAttribute($item, 'xfn'),
'href' => $this->getElAttribute($item, 'url')
);
$attr = apply_filters('nav_menu_link_attributes', $attr, $item, $args, $depth);
return (object)$attr;
}
public function getElAttribute($item, $name) {
return !empty($item->$name) ? $item->$name : '';
}
public function getElIdAttribute($item, $depth, $args) {
return esc_attr(apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth));
}
public function getElClassAttribute($item, $depth, $args) {
$class = empty($item->classes) ? array() : (array) $item->classes;
$class[] = 'menu-item-' . $item->ID;
return apply_filters( 'nav_menu_css_class', array_filter($class), $item, $args, $depth);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment