Adding a span to your Drupal 7 menu anchor tags

Steven Monetti
min read
April 24th, 2015

Introduction

Have you ever needed to customize the default Drupal menu  <a> tags? This quick-tip article will show you how to easily modify those tags, adding an additional  span. This will help in case you have big menu items but want to, for example, underline only the word.

Modify your theme

Drupal provides php functions to override how certain templates are rendered. These overrides can be easily added to your template.php file under sites/all/[your_theme]/template.php

Add a theme override

1. Edit  template.php

<?php 
/** 
 * @file * template.php 
 */ 
function THEME_link($variables) { 
   return '<a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' . drupal_attributes($variables['options']['attributes') . '><span>' . ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text'])) . '</span></a>';
}

* Make sure you replace THEME with your theme name.

2. Save and clear your theme registry cache.

Comments