Skip to content

Instantly share code, notes, and snippets.

@dragipostolovski
Created September 12, 2021 04:16
Show Gist options
  • Save dragipostolovski/27e4bb04c49e7ac43524c2b6bc7c4b44 to your computer and use it in GitHub Desktop.
Save dragipostolovski/27e4bb04c49e7ac43524c2b6bc7c4b44 to your computer and use it in GitHub Desktop.
<?php
/**
* Add menu meta box
*
* @param object $object The meta box object
*
* @link https://developer.wordpress.org/reference/functions/add_meta_box/
*/
function pe_custom_add_menu_meta_box( object $object ) {
add_meta_box( 'pe-menu-metabox', __( 'Tags' ), 'pe_custom_menu_meta_box', 'nav-menus', 'side' );
return $object;
}
add_filter( 'nav_menu_meta_box_object', 'pe_custom_add_menu_meta_box', 10, 1);
/**
* Displays a metabox for a tag menu item.
*
* @global int|string $nav_menu_selected_id (id, name or slug) of the currently-selected menu
*/
function pe_custom_menu_meta_box(){
global $nav_menu_selected_id;
$walker = new Walker_Nav_Menu_Checklist();
$current_tab = 'all';
if ( isset( $_REQUEST['tags-tab'] ) && 'wordpress' == $_REQUEST['tags-tab'] ) {
$current_tab = 'wordpress';
} elseif ( isset( $_REQUEST['tags-tab'] ) && 'custom' == $_REQUEST['tags-tab'] ) {
$current_tab = 'custom';
} elseif ( isset( $_REQUEST['tags-tab'] ) && 'all' == $_REQUEST['tags-tab'] ) {
$current_tab = 'all';
}
$tags = get_tags(array(
'taxonomy' => 'post_tag',
'orderby' => 'name',
'hide_empty' => false // for development
));
$wordpress = array();
$custom = array();
/* set values to required item properties */
foreach ( $tags as &$tag ) {
$tag->classes = array();
$tag->type = 'tag-custom';
$tag->object_id = $tag->term_id;
$tag->title = $tag->name;
$tag->object = 'tag-custom';
$tag->url = '';
$tag->attr_title = $tag->name;
if( false !== strpos( $tag->name , 'wordpress' ) ) {
$wordpress[] = $tag;
}
if( false !== strpos( $tag->name, 'custom' ) ) {
$custom[] = $tag;
}
}
$removed_args = array( 'action', 'customlink-tab', 'edit-menu-item', 'menu-item', 'page-tab', '_wpnonce' );
?>
<div id="tagsarchive" class="categorydiv">
<ul id="tags-tabs" class="tags-tabs add-menu-item-tabs">
<li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>>
<a class="nav-tab-link" data-type="tabs-panel-tags-all" href="<?php if ( $nav_menu_selected_id ) echo esc_url( add_query_arg( 'tags-tab', 'all', remove_query_arg( $removed_args ) ) ); ?>#tabs-panel-tags-all">
<?php _e( 'View All' ); ?>
</a>
</li>
<li <?php echo ( 'wordpress' == $current_tab ? ' class="tabs"' : '' ); ?>>
<a class="nav-tab-link" data-type="tabs-panel-tags-wordpress" href="<?php if ( $nav_menu_selected_id ) echo esc_url( add_query_arg( 'tags-tab', 'wordpress', remove_query_arg( $removed_args ) ) ); ?>#tabs-panel-tags-wordpress">
<?php _e( 'Wordpress' ); ?>
</a>
</li>
<li <?php echo ( 'custom' == $current_tab ? ' class="tabs"' : '' ); ?>>
<a class="nav-tab-link" data-type="tabs-panel-tags-custom" href="<?php if ( $nav_menu_selected_id ) echo esc_url( add_query_arg( 'tags-tab', 'custom', remove_query_arg( $removed_args ) ) ); ?>#tabs-panel-tags-custom">
<?php _e( 'Custom' ); ?>
</a>
</li>
</ul>
<div id="tabs-panel-tags-all" class="tabs-panel tabs-panel-view-all <?php echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>">
<ul id="authorarchive-checklist-all" class="categorychecklist form-no-clear">
<?php
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $tags), 0, (object) array( 'walker' => $walker) );
?>
</ul>
</div>
<div id="tabs-panel-tags-wordpress" class="tabs-panel tabs-panel-view-wordpress <?php echo ( 'wordpress' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>">
<ul id="tags-checklist-wordpress" class="categorychecklist form-no-clear">
<?php
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $wordpress), 0, (object) array( 'walker' => $walker) );
?>
</ul>
</div>
<div id="tabs-panel-tags-custom" class="tabs-panel tabs-panel-view-custom <?php echo ( 'custom' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>">
<ul id="tags-checklist-custom" class="categorychecklist form-no-clear">
<?php
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $custom), 0, (object) array( 'walker' => $walker) );
?>
</ul>
</div>
<p class="button-controls wp-clearfix">
<span class="list-controls">
<a href="<?php echo esc_url( add_query_arg( array( 'tags-tab' => 'all', 'selectall' => 1, ) )); ?>#tagsarchive" 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-tags-menu-item" id="submit-tagsarchive" />
<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