Skip to content

Instantly share code, notes, and snippets.

@sododesign
Last active April 14, 2016 18:23
Show Gist options
  • Save sododesign/6bfc60e68dcab784ed78 to your computer and use it in GitHub Desktop.
Save sododesign/6bfc60e68dcab784ed78 to your computer and use it in GitHub Desktop.
Wordpress Menu Metabox
<?php
// WORDPRESS MENU METABOX
function my_register_menu_metabox() {
$custom_param = array( 0 => 'This param will be passed to my_render_menu_metabox' );
add_meta_box( 'my-menu-metabox', 'Test Menu Metabox', 'my_render_menu_metabox', 'nav-menus', 'side', 'default', $custom_param );
}
add_action( 'admin_head-nav-menus.php', 'my_register_menu_metabox' );
/**
* Displays a menu metabox
*
* @param string $object Not used.
* @param array $args Parameters and arguments. If you passed custom params to add_meta_box(),
* they will be in $args['args']
*/
function my_render_menu_metabox( $object, $args ) {
global $nav_menu_selected_id;
// Create an array of objects that imitate Post objects
$my_items = array(
(object) array(
'ID' => 1,
'db_id' => 0,
'menu_item_parent' => 0,
'object_id' => 1,
'post_parent' => 0,
'type' => 'custom',
'object' => 'my-object-slug',
'type_label' => __('Home','italian-wedding-day'),
'title' => __('Home','italian-wedding-day'),
'url' => esc_url(home_url('/#wedding')),
'target' => '',
'attr_title' => '',
'description' => '',
'classes' => array('no-bg'),
'xfn' => '',
),
(object) array(
'ID' => 2,
'db_id' => 0,
'menu_item_parent' => 0,
'object_id' => 2,
'post_parent' => 0,
'type' => 'custom',
'object' => 'my-object-slug',
'type_label' => __('Married Couple','italian-wedding-day'),
'title' => __('Married Couple','italian-wedding-day'),
'url' => esc_url(home_url('/#couple')),
'target' => '',
'attr_title' => '',
'description' => '',
'classes' => array('no-bg'),
'xfn' => '',
),
(object) array(
'ID' => 3,
'db_id' => 0,
'menu_item_parent' => 0,
'object_id' => 3,
'post_parent' => 0,
'type' => 'custom',
'object' => 'my-object-slug',
'type_label' => __('Story','italian-wedding-day'),
'title' => __('Story','italian-wedding-day'),
'url' => esc_url(home_url('/#story')),
'target' => '',
'attr_title' => '',
'description' => '',
'classes' => array('no-bg'),
'xfn' => '',
),
);
$db_fields = false;
// If your links will be hieararchical, adjust the $db_fields array bellow
if ( false ) {
$db_fields = array( 'parent' => 'parent', 'id' => 'post_parent' );
}
$walker = new Walker_Nav_Menu_Checklist( $db_fields );
$removed_args = array(
'action',
'customlink-tab',
'edit-menu-item',
'menu-item',
'page-tab',
'_wpnonce',
); ?>
<div id="my-plugin-div">
<div id="tabs-panel-my-customlink-all" class="tabs-panel tabs-panel-active">
<ul id="my-plugin-checklist-pop" class="categorychecklist form-no-clear" >
<?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $my_items ), 0, (object) array( 'walker' => $walker ) ); ?>
</ul>
<p class="button-controls">
<span class="list-controls">
<a href="<?php
echo esc_url(add_query_arg(
array(
'my-customlink-all' => 'all',
'selectall' => 1,
),
remove_query_arg( $removed_args )
));
?>#my-menu-metabox" class="select-all"><?php _e( 'Select All' ); ?></a>
</span>
<span class="add-to-menu">
<input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-my-plugin-menu-item" id="submit-my-plugin-div" />
<span class="spinner"></span>
</span>
</p>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment