|

WordPress add class to menu anchor

WordPress has tools to add a class to an LI element of a menu item. It also has tools to add a rel value to the anchor of a menu item But it does not have tools to add a class to the anchor element of a menu item. Let say I add a phone number to a WordPress menu item with a tel anchor and I want to track phone clicks in Google Tag Manager as an event.

I can change the rel attribute to a class attribute:

//Change menu Link XFN from rel= to class=
function add_menuclass($ulclass) {
return preg_replace(‘/rel=”phone”/’, ‘class=”phone”‘, $ulclass, -1);
}
add_filter(‘wp_nav_menu’,’add_menuclass’);

By adding this code to my theme’s function.php file, now my Google Analytics Event tag will fire when this menu item is clicked.

Similar Posts