Last active
August 29, 2015 14:12
-
-
Save blpraveen/34938e76a0f65a635ed0 to your computer and use it in GitHub Desktop.
Wordpress MegaMenu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function megamenu_nav_update( $menu_id, $menu_item_db_id ) { | |
// verify this came from our screen and with proper authorization. | |
if ( ! isset( $_POST['megamenu-nonce'] ) || ! wp_verify_nonce( $_POST['megamenu-nonce'], 'megamenu-nonce-name' ) ) | |
return; | |
if(isset($_POST['mega-menu-colopt'][$menu_item_db_id]) && $_POST['mega-menu-colopt'][$menu_item_db_id] != '') { | |
update_post_meta( $menu_item_db_id, '_mega_menu_colopt', $_POST['mega-menu-colopt'][$menu_item_db_id]); | |
} else { | |
delete_post_meta( $menu_item_db_id, '_mega_menu_colopt' ); | |
} | |
} | |
function megamenu_custom_fields( $item_id, $item, $depth, $args ) { | |
$nav_menu_selected_id = isset( $_REQUEST['menu'] ) ? (int) $_REQUEST['menu'] : 0; | |
$_menu_object = ''; | |
$nav_menus = wp_get_nav_menus( array('orderby' => 'name') ); | |
$menu_count = count( $nav_menus ); | |
// Get recently edited nav menu | |
$recently_edited = absint( get_user_option( 'nav_menu_recently_edited' ) ); | |
$one_theme_location_no_menus = ( 1 == count( get_registered_nav_menus() ) && ! $add_new_screen && empty( $nav_menus ) && ! empty( $page_count->publish ) ) ? true : false; | |
$add_new_screen = ( isset( $_GET['menu'] ) && 0 == $_GET['menu'] ) ? true : false; | |
if ( empty( $recently_edited ) && is_nav_menu( $nav_menu_selected_id ) ) | |
$recently_edited = $nav_menu_selected_id; | |
// Use $recently_edited if none are selected | |
if ( empty( $nav_menu_selected_id ) && ! isset( $_GET['menu'] ) && is_nav_menu( $recently_edited ) ) | |
$nav_menu_selected_id = $recently_edited; | |
// On deletion of menu, if another menu exists, show it | |
if ( ! $add_new_screen && 0 < $menu_count && isset( $_GET['action'] ) && 'delete' == $_GET['action'] ) | |
$nav_menu_selected_id = $nav_menus[0]->term_id; | |
// Set $nav_menu_selected_id to 0 if no menus | |
if ( $one_theme_location_no_menus ) { | |
$nav_menu_selected_id = 0; | |
} elseif ( empty( $nav_menu_selected_id ) && ! empty( $nav_menus ) && ! $add_new_screen ) { | |
// if we have no selection yet, and we have menus, set to the first one in the list | |
$nav_menu_selected_id = $nav_menus[0]->term_id; | |
} | |
// If there's a menu, get its name. | |
if ( ! $nav_menu_selected_title && is_nav_menu( $nav_menu_selected_id ) ) { | |
$_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id ); | |
$nav_menu_selected_title = ! is_wp_error( $_menu_object ) ? $_menu_object->name : ''; | |
} | |
if ( 0 == $nav_menu_selected_id ) { | |
$new_menu_title = trim( esc_html( $_POST['menu-name'] ) ); | |
if ( $new_menu_title ) { | |
$_nav_menu_selected_id = wp_update_nav_menu_object( 0, array('menu-name' => $new_menu_title) ); | |
if ( is_wp_error( $_nav_menu_selected_id ) ) { | |
} else { | |
$_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id ); | |
$nav_menu_selected_id = $_nav_menu_selected_id; | |
} | |
} | |
} else { | |
$_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id ); | |
} | |
$menu_locations = get_nav_menu_locations(); | |
?> | |
<?php | |
if($_menu_object && $menu_locations['primary'] == $_menu_object->term_id && $depth == 1) { | |
?> | |
<input type="hidden" name="megamenu-nonce" value="<?php echo wp_create_nonce( 'megamenu-nonce-name' ); ?>" /> | |
<div class="description-wide" style="margin: 5px 0;"> | |
<span class="description"><?php _e( "Column Select (Mega Menu)", 'nav-menu-roles' ); ?></span> | |
<br /> | |
<?php | |
$mega_colpot = get_post_meta( $item->ID, '_mega_menu_colopt', true ); | |
$column_options = array('full' => 'Full Column','col4' => 'Fourth Column','col3' => 'Third Column','col2' => 'Second Column','col1' => 'First Column', 'item' => 'Menu Item'); | |
if(!$mega_colpot) { | |
$mega_colpot = 'col1'; | |
} | |
?> | |
<div class="role-input-holder" style="float: left; width: 100%; margin: 2px 0;"> | |
<select name="mega-menu-colopt[<?php echo $item->ID ;?>]" id="mega-menu-colopt-<?php echo $item->ID ;?>" > | |
<?php foreach($column_options as $key => $colopt) { | |
$select_colopt = selected( true, $mega_colpot == $key , false ); | |
?> | |
<option value="<?php echo $key;?>" <?php echo $select_colopt; ?>><?php echo $colopt;?></option> | |
<?php } ?> | |
</select> | |
</div> | |
</div> | |
<?php | |
} | |
} | |
add_action( 'wp_update_nav_menu_item', 'megamenu_nav_update', 10, 2 ); | |
add_action( 'wp_nav_menu_item_custom_fields', 'megamenu_custom_fields', 10, 4); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.mega-menu , .mega-menu * { | |
margin: 0; | |
padding: 0; | |
list-style: none; | |
} | |
.mega-menu { | |
line-height: 1.0; | |
} | |
.mega-menu > li { | |
padding-bottom: 10px; | |
} | |
.mega-menu ul.sub-menu a:hover { | |
color:#222; | |
text-decoration:underline; | |
} | |
.mega-menu ul.mega-submenu-item > li { | |
min-width: 200px; | |
} | |
.mega-menu ul.mega-submenu-item > li > a { | |
margin-top:15px; | |
color:#ff0f00; | |
} | |
.mega-menu ul.sub-menu .sub-menu-item:first-child { | |
margin-top:15px; | |
} | |
.mega-menu ul.sub-menu .sub-menu-item { | |
min-width:200px; | |
} | |
.mega-menu ul.sub-menu > li > a:hover { | |
color:#f58b00; | |
} | |
.mega-menu ul.sub-menu a { | |
font-size:12px; | |
color:#999; | |
font-weight:normal; | |
line-height:12px; | |
padding:4px 2px; | |
} | |
.mega-menu .mega-submenu-col { | |
display:table-cell; | |
} | |
.mega-menu .mega-submenu-col > ul { | |
margin:0px !important; | |
} | |
.mega-menu ul.sub-menu { | |
position: absolute; | |
width: auto; /* left offset of submenus need to match (see below) */ | |
max-width: 980px; | |
top: -999em; | |
z-index: 200; | |
padding: 0px 20px 20px 20px; | |
display:table; | |
} | |
.mega-menu li { | |
float:left; | |
} | |
.mega-menu li:hover { | |
visibility: inherit; /* fixes IE7 'sticky bug' */ | |
} | |
.mega-menu a { | |
display: block; | |
position: relative; | |
} | |
.mega-menu ul li { | |
float:none; | |
width: auto; | |
} | |
.mega-menu li:hover { | |
visibility: inherit; /* fixes IE7 'sticky bug' */ | |
} | |
.mega-menu .sub-menu { | |
-webkit-transition: all 0s ease; | |
transition: all 0s ease; | |
} | |
.mega-menu .sub-menu li li { | |
width:250px; | |
float:none; | |
display:block; | |
} | |
.mega-menu li:hover .sub-menu { | |
-webkit-transition-delay: .25s; | |
transition-delay: .25s; | |
} | |
.mega-menu li:hover .sub-menu { | |
top: 39px; | |
} | |
.mega-menu .sub-menu { | |
background:#fff; | |
width:auto; | |
box-shadow: 0 4px 6px 3px #bababa; | |
min-height:350px; | |
} | |
.mega-menu .sub-menu li { | |
} | |
.mega-menu a { | |
padding:8px 10px 7px 10px; | |
text-decoration:none; | |
cursor:pointer; | |
font-size:12px; | |
text-transform:uppercase; | |
font-weight:900; | |
line-height:1.3em; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment