Skip to content
  • Quick Ref
  • Contact
  • About
wpcanyon.com

wpcanyon.com

Tag: Menus

Add a CSS class to the current menu item (wp_nav_menu)

Posted on August 19, 2025 By Admin No Comments on Add a CSS class to the current menu item (wp_nav_menu)

Add a CSS Class to the Current Menu Item (wp_nav_menu)

When working with WordPress menus, you might want to highlight the current menu item by adding a custom CSS class. This is useful for styling purposes, such as changing the color or font of the active menu item to improve user navigation experience. WordPress automatically adds some classes like current-menu-item, but sometimes you need a custom class for more precise control or specific styling frameworks.

Quick Fix: Add a Custom CSS Class to the Current Menu Item

  1. Open your theme’s functions.php file or create a mini-plugin.
  2. Add a filter to nav_menu_css_class to append your custom class to the current menu item.
  3. Save and test the menu on the front end to confirm the class is added.

Why This Happens

WordPress’s wp_nav_menu() function generates menu HTML and automatically adds classes like current-menu-item to the active menu item. However, these default classes might not fit your CSS framework or design system. By hooking into the nav_menu_css_class filter, you can add or modify classes dynamically, giving you full control over menu item styling.

Step-by-Step: Add a Custom CSS Class to the Current Menu Item

  1. Open your WordPress theme folder and locate the functions.php file.
  2. Add the following code snippet to the end of the file:
function add_custom_class_to_current_menu_item($classes, $item) {
    if (in_array('current-menu-item', $classes)) {
        $classes[] = 'my-custom-current-class'; // Replace with your custom class name
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'add_custom_class_to_current_menu_item', 10, 2);
  1. Save the file and upload it back to your server if editing locally.
  2. Visit your website and inspect the menu. The current menu item should now have the my-custom-current-class added alongside the default classes.

Alternative: Create a Mini-Plugin for This

If you prefer not to modify your theme’s functions.php, create a mini-plugin instead:

<?php
/*
Plugin Name: Custom Current Menu Class
Description: Adds a custom CSS class to the current menu item.
Version: 1.0
Author: Your Name
*/

function add_custom_class_to_current_menu_item($classes, $item) {
    if (in_array('current-menu-item', $classes)) {
        $classes[] = 'my-custom-current-class'; // Replace with your custom class name
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'add_custom_class_to_current_menu_item', 10, 2);

Save this as custom-current-menu-class.php and upload it to wp-content/plugins/. Activate it from the WordPress admin dashboard.

Testing

  • Clear any caching plugins or server cache to see changes immediately.
  • Open your site in a browser and navigate to different pages.
  • Use browser developer tools (Inspect Element) to verify the custom class appears on the current menu item.
  • Apply CSS rules targeting .my-custom-current-class to confirm styling changes.

Variations

  • Add class to current menu ancestor: To highlight parent items of the current page, check for current-menu-ancestor or current-menu-parent classes.
  • Different class names: Change my-custom-current-class to any class name that fits your CSS naming conventions.
  • Multiple classes: Append multiple classes by adding more entries to the $classes array.
  • Conditional logic: Add classes only for specific menus by checking $item-menu-slug or $item-menu_item_parent.
function add_custom_class_to_current_menu_item($classes, $item, $args) {
    if (in_array('current-menu-item', $classes)) {
        $classes[] = 'my-custom-current-class';
    }
    if (in_array('current-menu-ancestor', $classes)) {
        $classes[] = 'my-custom-ancestor-class';
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'add_custom_class_to_current_menu_item', 10, 3);

Works On

Environment Notes
Apache Fully compatible; no special configuration needed.
Nginx Works without issues; ensure PHP is properly configured.
LiteSpeed Compatible; same as Apache.
cPanel / Plesk Works as expected; edit files via File Manager or FTP.
Any WordPress Theme Compatible with all themes that use wp_nav_menu().

FAQ

Q: Why doesn’t my custom class show up on the current menu item?
A: Make sure you cleared caches and that your theme uses wp_nav_menu(). Also, confirm the filter is added correctly and no syntax errors exist.
Q: Can I add this class only to a specific menu?
A: Yes. Use the third parameter $args in the filter callback to check $args-theme_location or $args-menu and conditionally add classes.
Q: How do I style the new class?
A: Add CSS rules targeting your custom class in your theme’s stylesheet or customizer. For example: .my-custom-current-class { color: red; }
Q: Will this work with custom walker classes?
A: Yes, but if your custom walker overrides menu classes, ensure it merges or respects the classes added by this filter.
…
Developer Snippets

Adding Classes to the Current Page Item in wp_nav_menu()

Posted on August 19, 2025 By Admin No Comments on Adding Classes to the Current Page Item in wp_nav_menu()

Adding Classes to the Current Page Item in wp_nav_menu()

If you want to add a custom class to the current menu item in WordPress navigation, you’ve come to the right place. By default, WordPress adds classes like current-menu-item to highlight the active page in menus, but sometimes you need to add your own classes for styling or JavaScript targeting. This tutorial shows you how to add class to current menu item WordPress easily and cleanly.

Quick Fix: Add a Custom Class to the Current Menu Item

  1. Hook into the nav_menu_css_class filter.
  2. Check if the menu item is the current page.
  3. Add your custom class to the array of classes.
  4. Return the modified classes array.

This approach works perfectly in modern WordPress and can be added to your functions.php or a small custom plugin.

Why You Should Add a Custom Class to the Current Menu Item

  • Custom Styling: Sometimes the default current-menu-item class isn’t enough for your CSS needs.
  • JavaScript Targeting: Adding a unique class makes it easier to target the current menu item with scripts.
  • Theme Compatibility: Some themes or page builders require specific classes for active states.
  • Better Control: You can add multiple classes or change the class name to fit your project.

When to Use This Method

  • You want to add a class beyond the default WordPress classes.
  • You are building a custom theme or child theme and need precise control over menu item classes.
  • You want to add JavaScript hooks or CSS animations to the current menu item.
  • You are not using block-based navigation or want a PHP-based solution.

Updated Code for Modern WordPress

The best way to add a class to the current menu item is by using the nav_menu_css_class filter. Here’s a clean, modern example:

function add_custom_class_to_current_menu_item($classes, $item) {
    if (in_array('current-menu-item', $classes, true)) {
        $classes[] = 'my-custom-current-class';
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'add_custom_class_to_current_menu_item', 10, 2);

This code checks if the menu item already has the current-menu-item class and appends your custom class my-custom-current-class.

How to Add This via functions.php or a Small Plugin

To add this code, you can either place it in your theme’s functions.php file or create a small plugin.

Option 1: Add to functions.php

  1. Open your active theme folder.
  2. Locate and open functions.php.
  3. Paste the following code at the end:
function add_custom_class_to_current_menu_item($classes, $item) {
    if (in_array('current-menu-item', $classes, true)) {
        $classes[] = 'my-custom-current-class';
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'add_custom_class_to_current_menu_item', 10, 2);
  1. Save the file and refresh your site to see the changes.

Option 2: Create a Small Plugin

  1. Create a new folder inside wp-content/plugins called custom-current-menu-class.
  2. Create a file named custom-current-menu-class.php inside this folder.
  3. Paste this code into the file:
<?php
/*
Plugin Name: Custom Current Menu Class
Description: Adds a custom class to the current menu item.
Version: 1.0
Author: Your Name
*/

function add_custom_class_to_current_menu_item($classes, $item) {
    if (in_array('current-menu-item', $classes, true)) {
        $classes[] = 'my-custom-current-class';
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'add_custom_class_to_current_menu_item', 10, 2);
  1. Go to WordPress admin > Plugins and activate “Custom Current Menu Class.”
  2. Check your menu on the front end.

Step-by-Step Test

  1. Apply the code via functions.php or plugin as shown above.
  2. Go to Appearance > Menus and ensure your menu is assigned to a theme location.
  3. Visit your site’s front end and navigate to a page included in the menu.
  4. Inspect the menu item using browser developer tools.
  5. Look for the default current-menu-item class plus your custom class my-custom-current-class.
  6. Test your CSS or JavaScript targeting the new class.

Block Themes & Gutenberg Notes

With the rise of block themes and Gutenberg’s Navigation block, the way menus are rendered can differ:

  • Block Themes: The Navigation block outputs menus dynamically and may not use wp_nav_menu() filters.
  • Gutenberg Navigation Block: Custom classes for current items are handled internally or via block settings.
  • Workaround: For block themes, consider using wp_nav_menu_items filter or custom block styles.
  • Classic Themes: The method described here works perfectly for PHP-rendered menus.

Common Pitfalls

  • Class Not Appearing: Ensure your menu is properly assigned and the page is part of the menu.
  • Wrong Hook: Use nav_menu_css_class, not wp_nav_menu_items for adding classes to individual items.
  • Block Themes: This method may not work if your theme uses block-based navigation.
  • Cache Issues: Clear any caching plugins or server cache to see changes immediately.
  • Class Name Conflicts:
…
WordPress Snippets

Recent Posts

  • Top WordPress Themes for Blogs in 2025
  • WordPress Admin Panel Trick: Adding ID Field to the Posts Listing
  • Solution previous_posts_link and next_posts_link Not Working
  • Show Top Commentators in WordPress Without a Plugin
  • How to Style Admin Comments in WordPress

Recent Comments

    Archives

    • August 2025

    Categories

    • Admin & Blocks
    • Admin & UI
    • Automation
    • Automation & Plugins
    • Comments
    • Comparisons
    • Database & Revisions
    • Developer Snippets
    • Fixes & Errors
    • Media & Thumbnails
    • Queries & Pagination
    • Security
    • Speed & Security
    • Tips & Tricks
    • WooCommerce How‑tos
    • WordPress Snippets
    • WordPress Themes
    • Terms & Conditions
    • Affiliate Disclosure

    Copyright © 2025 wpcanyon.com.

    Powered by PressBook WordPress theme

    Also by the maker of MySurveyReviews.com