Skip to content

Instantly share code, notes, and snippets.

@donnykurnia
Created December 10, 2012 18:15
Show Gist options
  • Save donnykurnia/4252244 to your computer and use it in GitHub Desktop.
Save donnykurnia/4252244 to your computer and use it in GitHub Desktop.
Wordpress menu generator
// import socialniho menu
$top = new Menu('Top menu');
// page link
if ($page = get_page_by_path('some-page')) {
$top->title('Page');
$top->object_id($page->ID);
$top->object('page');
$top->type('post_type');
$top->save();
} else {
throw new Exception('Stranka nálepky neexistuje');
}
// custom post example
$top->title('Title')->url('/url')->description('Some link')->save();
$top->title('URL')->url('http://www.URL.com/')->description('Desc')->target('_blank')->save();
// categories
$cat_parent_id = $top->title('Categories')->url('/clanky')->save();
foreach (get_categories() as $category) {
$top->parent_id($cat_parent_id);
$top->title($category->name);
$top->description($category->description);
// link to objects
$top->object_id($category->term_id);
$top->object('category');
$top->type('taxonomy');
$top->save();
}
<?php
/**
* From: https://gist.github.com/4148529
* @method Menu db_id
* @method Menu object_id
* @method Menu object
* @method Menu parent_id
* @method Menu position
* @method Menu type
* @method Menu title
* @method Menu url
* @method Menu description
* @method Menu attr-title
* @method Menu target
* @method Menu classes
* @method Menu xfn
* @method Menu status
*/
class Menu {
/** @var null */
public $id = null;
/** @var null */
public $menu = null;
/** @var int */
public $position = 1;
/** @var array */
public $item = null;
public $is_new_menu = false;
/**
* @param $name
* @param bool $delete
*/
public function __construct($name, $delete = true) {
if ($delete) wp_delete_nav_menu($name);
if (is_nav_menu($name)) {
$this->menu = wp_get_nav_menu_object($name);
$this->id = (int)$this->menu->term_id;
} else {
$this->id = (int)wp_create_nav_menu($name);
$this->menu = wp_get_nav_menu_object($name);
$this->is_new_menu = true;
}
}
/**
* @return object
*/
public function save() {
// upravim pole pred save
$this->item['menu-item-position'] = isset($this->item['menu-item-position']) ? $this->item['menu-item-position'] : $this->position++;
$this->item['menu-item-status'] = isset($this->item['menu-item-status']) ? $this->item['menu-item-status'] : 'publish';
$this->item['menu-item-type'] = isset($this->item['menu-item-type']) ? $this->item['menu-item-type'] : 'custom';
$id = wp_update_nav_menu_item($this->id, 0, (array)$this->item);
$this->item = array(); // smazu po ulozeni
// WILL be fix in new Wordpess 3.5
// FIXME https://github.com/WordPress/WordPress/commit/ae96b842f9f55ecfb22da705a4902b9d25580259#wp-includes/nav-menu.php
if ($this->id && (!is_object_in_term($id, 'nav_menu', (int)$this->id))) {
wp_set_post_terms($id, array((int)$this->id), 'nav_menu');
}
return wp_setup_nav_menu_item($id);
}
/**
* @param string $location
*/
public function setLocation($location = 'primary') {
$locations = get_nav_menu_locations();
$locations[$location] = $this->menu->term_id;
set_theme_mod('nav_menu_locations', $locations);
}
/**
* @return array|WP_Error
*/
public static function getAllMenus() {
return get_terms('nav_menu', array('hide_empty' => true));
}
/**
* @param $name
* @param $value
* @return Menu
*/
public function __call($name, $value) {
$this->item['menu-item-' . str_replace('_', '-', $name)] = reset($value);
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment