Instantly share code, notes, and snippets.
Created
January 9, 2025 04:31
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save benpearson/3ef89b8feefa7e966bb8160538669d66 to your computer and use it in GitHub Desktop.
WordPress: Examples of menu filters (including nav menu widgets)
This file contains hidden or 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
<?php | |
/** | |
* Menu: Filters | |
*/ | |
/** | |
* WP_Nav_Menu_Widget: Edit params to add wrapping HTML | |
*/ | |
function dt_filter_dynamic_sidebar_params($params) | |
{ | |
if ($params[0]['widget_name'] == "Navigation Menu") { | |
$params[0]['before_widget'] = '<div class="section__aside-inner">'; | |
$params[0]['after_widget'] = '</div><!-- /.section__aside-inner -->'; | |
$params[0]['before_title'] = '<h3>'; | |
$params[0]['after_title'] = '</h3>'; | |
} | |
return $params; | |
} | |
// add_filter( 'dynamic_sidebar_params', 'dt_filter_dynamic_sidebar_params' ); | |
/** | |
* WP_Nav_Menu_Widget: Edit wp_nav_menu args | |
* | |
* Among other things, this filter is used to add an identifying class to container | |
* for use in other menu filters | |
*/ | |
function dt_filter_widget_nav_menu_args($nav_menu_args, $nav_menu, $args, $instance) | |
{ | |
if ($args['widget_name'] == 'Navigation Menu') { | |
$nav_menu_args['container_class'] = 'section__aside-menu dt-filter-tag__nav-menu-widget'; // dt-filter-tag needed for other filters | |
$nav_menu_args['walker'] = new Walker_Nav_Menu(); // needed to access has_children property in menu filters | |
$nav_menu_args['container'] = 'nav'; | |
} | |
return $nav_menu_args; | |
}; | |
// add_filter( 'widget_nav_menu_args', 'dt_filter_widget_nav_menu_args', 10, 4 ); | |
/** | |
* Nav menus: Add classes to <li> elements | |
*/ | |
function dt_filter_nav_menu_css_class($array, $item, $args, $depth) | |
{ | |
/** | |
* WP_Nav_Menu_Widget: this filter works in conjuction with the | |
* widget_nav_menu_args filter using the identifying container class | |
* dt-filter-tag__nav-menu-widget | |
*/ | |
if ( | |
$args->container_class | |
&& (strpos($args->container_class, 'dt-filter-tag__nav-menu-widget') !== false) | |
&& ($depth == 0) | |
) { | |
if ($args->walker->has_children === true) { | |
$array[] = 'has-submenu js-equal-heights-parent'; | |
} | |
} | |
return $array; | |
}; | |
// add_filter( 'nav_menu_css_class', 'dt_filter_nav_menu_css_class', 10, 4 ); | |
/** | |
* Nav menus: Add classes to <a> elements | |
*/ | |
function dt_filter_nav_menu_link_attributes($atts, $item, $args, $depth) | |
{ | |
/** | |
* WP_Nav_Menu_Widget: Add class to links | |
* | |
* This filter works in conjuction with the | |
* widget_nav_menu_args filter using the identifying container class | |
* dt-filter-tag__nav-menu-widget | |
*/ | |
if ($args->container_class && strpos($args->container_class, 'dt-filter-tag__nav-menu-widget') !== false) { | |
if ($depth == 0) { | |
$atts['class'] = 'js-equal-height'; | |
} | |
} | |
$contact_page_id = 13; // TODO Update if used | |
// Main menu: Add extra classes to top level links | |
if (('primary' == $args->theme_location) && | |
(0 == $depth) | |
) { | |
$atts['class'] = 'btn btn--transparent'; | |
// Add class to Contact menu item | |
if ($contact_page_id == $item->object_id) { | |
$atts['class'] .= ' btn--bordered'; | |
} | |
} | |
// Secondary footer menu: Add extra classes to Contact menu item | |
if (('footer-secondary' == $args->theme_location) && | |
($contact_page_id == $item->object_id) | |
) { | |
$atts['class'] = 'btn btn--transparent btn--bordered'; | |
} | |
return $atts; | |
}; | |
// add_filter( 'nav_menu_link_attributes', 'dt_filter_nav_menu_link_attributes', 10, 4 ); | |
/** | |
* Nav menus: Add class to submenu <ul> elements | |
*/ | |
function dt_filter_nav_menu_submenu_css_class($classes, $args, $depth) | |
{ | |
/** | |
* WP_Nav_Menu_Widget: this filter works in conjuction with the | |
* widget_nav_menu_args filter using the identifying container class | |
* dt-filter-tag__nav-menu-widget | |
*/ | |
if ($args->container_class && strpos($args->container_class, 'dt-filter-tag__nav-menu-widget') !== false) { | |
$classes = ['sub-menu-alt']; | |
} | |
// Change submenu class in primary menu location | |
if ('primary' == $args->theme_location) { | |
$classes = ['dropdown']; | |
} | |
return $classes; | |
} | |
// add_filter( 'nav_menu_submenu_css_class', 'dt_filter_nav_menu_submenu_css_class', 10, 3 ); | |
/** | |
* Nav menu: Edit wp_nav_menu html | |
*/ | |
function dt_filter_nav_menu_widget_edit_html($array, $int) | |
{ | |
/** | |
* WP_Nav_Menu_Widget: this filter works in conjuction with the | |
* widget_nav_menu_args filter using the identifying container class | |
* dt-filter-tag__nav-menu-widget | |
*/ | |
if ($args->container_class && strpos($args->container_class, 'dt-filter-tag__nav-menu-widget') !== false) { | |
// Replace containers start | |
$array = str_replace('<nav class="dt-nav-menu-widget">', '<div class="widget__body"><nav class="nav-vertical">', $array); | |
// Replace containers end | |
$array = str_replace('</nav>">', '</nav><!-- /.nav-vertical --></div><!-- /.widget__body -->', $array); | |
// Add 'current' class | |
$array = str_replace('current', 'current current', $array); | |
} | |
return $array; | |
}; | |
// add_filter( 'wp_nav_menu', 'dt_filter_nav_menu_widget_edit_html', 10, 2 ); | |
/** | |
* Nav menu: Edit args for a nav menu item | |
*/ | |
function dt_filter_nav_menu_item_args($args, $item, $depth) | |
{ | |
// Add spans before <a> tags on top level menu items in primary menu location | |
if ('primary' == $args->theme_location) { | |
if (0 == $depth) { | |
$args->before = '<span class="trigger-dropdown"></span>'; | |
} else { | |
$args->before = ''; | |
} | |
} | |
return $args; | |
} | |
// add_filter( 'nav_menu_item_args', 'dt_filter_nav_menu_item_args', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment