Connect with us

Wordpress Tips

[SOLVED] Remove Extra Class and ID from WordPress Menu

Bro Kundan



As you know WordPress comes with many defaults extra id and classes which make our website heavy which increases the loading time of our website, in order to optimize your WordPress you have removed these extra ids and classes.

Before optimize

<div class=”main-navigation“>
<ul id=”menu-primary” class=”menu”>
<li id=”menu-item-14″ class=”menu-item menu-item-type-post_type menu-item-object-page menu-item-14″></li>
<li id=”menu-item-16″ class=”menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16″></li>
<li id=”menu-item-20″ class=”menu-item menu-item-type-post_type menu-item-object-page menu-item-20″></li>
<li id=”menu-item-15″ class=”menu-item menu-item-type-taxonomy menu-item-object-category menu-item-15″></li>
<li id=”menu-item-17″ class=”menu-item menu-item-type-custom menu-item-object-custom menu-item-17″></li>

After optimize

<div class=”main-navigation“>
<ul id=”menu-primary” class=”menu”>

How to Remove Extra Classes and IDs from Wordpress Menu (wp_nav_menu)

In order to remove extra id and class, you have to just these PHP code inside your themes function.php file

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() : '';

then click on update file

Now delete cache file if you are using cache plugins then refresh page and then see source code of your website view-source:

See Screen Shorts As Demo

Remove Extra Class and ID from WordPress Menu

Click to comment

Leave a Reply

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