Skip to content
PHP

How to add active class in WordPress bootstrap menu using hook

A
Author
Jun 27, 2020 · 3 min read
How to add active class in  WordPress bootstrap menu using hook

When building websites with WordPress and Bootstrap, one of the most common tasks you might encounter is adding an active class to the menu items when they are clicked or visited by the user. This is important for user navigation and helps in highlighting the current section or page they are on, offering a better user experience.

Bootstrap, which is one of the most popular front-end frameworks, makes it easy to create beautiful, responsive menus. However, by default, WordPress does not automatically add the “active” class to the menu item that corresponds to the current page. Fortunately, WordPress provides a convenient way to modify the output of WordPress menus through filters and hooks. In this blog, we’ll guide you on how to use the nav_menu_css_class filter to add the “active” class to WordPress Bootstrap menus.


What is the Active Class?

Before diving into the technicalities, let’s first understand the role of the “active” class in web development.

Bootstrap’s Active Class

In Bootstrap, the “active” class is used to highlight a menu item, indicating that it’s the current page or section the user is on. This class is typically applied to navigation links, tabs, buttons, and other elements to provide a visual cue to the user.

For instance, when a user clicks on a menu item, that item should be highlighted with the active class to indicate the current page. This helps users know where they are on the website and allows them to quickly navigate between sections.

Why Use Hooks to Modify WordPress Menus?

WordPress provides a set of built-in functions, hooks, and filters that developers can use to modify the default behavior and output of the platform. This allows you to customize WordPress functionality without modifying the core files, ensuring your customizations stay intact when updating WordPress.

In this blog, we will be using the nav_menu_css_class filter hook to add the “active” class to the menu items. This filter allows us to modify the CSS classes assigned to menu items, so when the current page matches a menu item, we can add the “active” class to it.


How WordPress Menus Work

WordPress uses a built-in function to generate menus, which are based on custom menus defined by the user. The menus are output using the wp_nav_menu() function, and this function automatically adds specific classes to menu items, such as menu-item, menu-item-type-post_type, current-menu-item, etc.

The current-menu-item class is automatically applied to the active page’s menu item, meaning that WordPress already detects the active page and adds a class. However, the default WordPress output does not add the Bootstrap-specific “active” class.

By using WordPress hooks and filters, you can enhance the existing output by adding the “active” class from Bootstrap to the correct menu item, allowing you to use Bootstrap’s styling and functionality effectively.

The process involves using the nav_menu_css_class filter hook to modify the classes assigned to menu items and checking if a menu item corresponds to the current page or section.

Here’s a simple solution that will automatically add the “active” class to the current menu item:

add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);

function special_nav_class ($classes, $item) {
if (in_array('current-menu-item', $classes) ){
$classes[] = ‘active ‘;
}
return $classes;
}

A
Author

Author at The Creative Coders.

The monthly build log

One email a month. New posts, lessons, and the occasional strong opinion. No spam.