HomeTips & Tricks Wp_nav_menu() Function: How to Remove Class and IDs

Wp_nav_menu() Function: How to Remove Class and IDs

Hi, if you want to remove and clean unnecessary classes and IDs from WordPress site being added while using custom navigation menus? So then here’s a some function code to remove them:

You can see the above figure example code: Here is class and id we have not removed.

As you can see example from the above image, The screenshot taken of the page source all those long classes and IDs. and after cleaning up the wp_nav_menu(), Here is the image below shows after cleaner menu.

INSTRUCTION:

Step 1: First login to your WP Dashboard with your current username and Password, Then go to the directly into “Appereance” and “Theme Editor” and find the functions.php file and click on that functions.php.

Step 2: Now paste the below code into your “functions.php” this should remove all the nasty classes and IDs from your wp_nav_menu

#Remove menu Class And ID
add_filter(‘nav_menu_css_class’, ‘my_css_attributes_filter’, 100, 1);
add_filter(‘nav_menu_item_id’, ‘my_css_attributes_filter’, 100, 1);
add_filter(‘page_css_class’, ‘my_css_attributes_filter’, 100, 1);
function my_css_attributes_filter($var) {
return is_array($var) ? array_intersect($var, array(‘current-menu-item’)) : ”;
}

Finally click on “Update File” and save your work, Now you can go your public website and view the source code press by “CTRL+U” from keyword maybe there has removed WP Menu classes and IDs.

In the end

I hope you have found this article helpful. Let us me your opinion or questions if any through the comment form in below or use this form to ask your question.

You May Also Like

About the Author: Kedar Adhikari

I am WordPress Themes developer and designer and also any type of website design with minimum cost. for more information you can contact me on email [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2024 My WP Helper. All rights reserved.