Skip to content

Instantly share code, notes, and snippets.

@BruceMcKinnon
Created September 14, 2023 06:57
Show Gist options
  • Save BruceMcKinnon/10464f146b15bcbaee26670ae87952a3 to your computer and use it in GitHub Desktop.
Save BruceMcKinnon/10464f146b15bcbaee26670ae87952a3 to your computer and use it in GitHub Desktop.
WP menu custom container
//
// Custom WP menu container - thanks to https://gist.github.com/nikolov-tmw/8698598
//
// Add a custom container to your
//
add_action( 'admin_head-nav-menus.php', 'custom_register_menu_metabox' );
function custom_register_menu_metabox() {
$custom_param = array( 0 => 'mega_col' );
add_meta_box( 'custom-nav-container-metabox', 'Custom Nav Container', 'custom_nav_container_metabox', 'nav-menus', 'side', 'default', $custom_param );
}
function custom_nav_container_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_nav_container',
'object' => 'custom-nav-container',
'type_label' => 'Custom Nav Container',
'title' => 'Custom Nav Container',
'url' => home_url( '#' ),
'target' => '',
'attr_title' => '',
'description' => '',
'classes' => array(),
'xfn' => '',
),
);
$db_fields = false;
// If your links will be hieararchical, adjust the $db_fields array below
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="custom-container-div">
<div id="tabs-panel-custom-container-all" class="tabs-panel tabs-panel-active">
<ul id="custom-container-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(
'custom-container-all' => 'all',
'selectall' => 1,
),
remove_query_arg( $removed_args )
));
?>#my-menu-test-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-custom-container-menu-item" id="submit-custom-container-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