• Skip to content
  • Skip to primary sidebar

Michael Rassel

Web Developer

WordPress add class to menu anchor

September 13, 2016 by Michael Rassel

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.

Filed Under: Web Dev, Wordpress Tagged With: Wordpress menu class

Primary Sidebar

Recent Posts

  • Gravity Forms Google Analytics tracking in Tag Manager
  • SSL htaccess redirect
  • CSS Clearfix with Pseudo Selector
  • Woocommerce Logged in Only
  • Repsonsive Video embed
  • Add robots meta tag to specific page
  • Youtube Responsive JS embed
  • Disable Cache while Chrome Developer Tools is open
  • Import CSV to PHPMyAdmin
  • Adwords Call Forwarding testing
  • Google Tag Manager in Genesis Theme
  • Woocommerce Widget Featured Product by Category
  • WordPress Disable Password Change Nofitication
  • Reposition Genesis header right widget
  • Center text over image
  • JS Full Window Height
  • Allow Iframe but restrict direct access
  • Show excerpt on Woocommerce archive list
  • Woocommerce default shipping selection
  • Remove Images from WooCommerce pages

Tags

Adwords Call Conversion Logged in users only Open Office remove empty rows role restriction Tag Manager web design terms Woocommerce Wordpress menu class

Categories

  • Music (2)
  • Web Dev (36)
    • Magento (1)
    • Wordpress (15)

Copyright © 2019 · Magazine Pro on Genesis Framework · WordPress · Log in