Customising theme_link to always add a relevant title tag for the SEO of your site

Customising theme_link to always add a relevant title tag for the SEO of your site

When building this site I wanted the links to all have the site name in the title tag as the keywords in the site name are what I want to be indexed for by Google. To do this I have made a simple theme_link function override to append the site name to the title tag if it is not already present.  

While I was doing this I also noticed that not all of my links had the title of the page they were linking to in the title tag and as I didn't want to trawl through all of my code to try to find where I had been lazy and not added these in manually I also added the functionality to this theme link function to get the title of the page we are linking to and insert this in to the title tag as well. 

The results are pretty good, all the links on this site are run through this function so you can see the results for your self, I am hoping this will help with the SEO!

Below is the PHP code to do this that you would have to add to your theme template.php in your Drupal 7 installation. 

 * Overrides the theme_link function to add the page title and site name to
 * all links, even if not provided.
 * @param array $variables
 * @return string
function mytheme_link($variables) {
  // Add the site name variable to the end of link titles for SEO
  static $site_name;
  if (empty($site_name)) {
    $site_name = variable_get('site_name');
  $attributes = $variables['options']['attributes'];
  $text = trim(check_plain(strip_tags($variables['text'])));
  if (!empty($attributes['title'])) {
    if (mb_strpos($attributes['title'], $site_name) === FALSE) {
      $attributes['title'] .= " | " . $site_name;
  else {
    $normal_path = drupal_get_normal_path($variables['path']);
    $item = menu_get_item($normal_path);
    if (!empty($item['title'])) {
      $attributes['title'] = $item['title'] . " | " . $site_name;
    elseif (!empty($text)) {
      $attributes['title'] = $text . " | " . $site_name;
    else {
      $attributes['title'] = $site_name;
  return '<a href="' . check_plain(
  ) . '"' . drupal_attributes($attributes) . '>' .
    ($variables['options']['html'] ?
    $variables['text'] : check_plain($variables['text'])
  ) . '</a>';



what are some superior and in demand websites for blogs? ??

Hi John, If you ask me I am going to say Drupal as this is what I work with but there are also arguments for WordPress, Joomla and some others.

It just depends on what you want, what skills you have and your budget.


Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.