Menu icon
RedCrackle
Menu icon
Services
01Design Transformation
About UsCase StudiesBlogContact Us

<

Blog post

How to override theme functions in Drupal 7

Neerav Mehta

Founder & CEO

Before overriding theme functions, let us understand the concept of theme function.

In Drupal, a theme function is a PHP function that is used to output the HTML of any Drupal object. These functions are prefixed with theme_. All the functions that produce HTML are themeable and are invoked using theme() rather than being directly called.

We can override theme functions in template.php.

  1. Find out the theme function which produces the output that you want to change.
  2. Copy the code and paste it into template.php
  3. Rename the function. "theme_functionname()" will be renamed as "YourThemeName_functionname()". For eg: "theme_username()" will be renamed as "yourthemename_username()".
  4. Modify the output.

For example:

function theme_username($variables) {
  if (isset($variables['link_path'])) {
    // We have a link path, so we should generate a link using l().
    // Additional classes may be added as array elements like
    // $variables['link_options']['attributes']['class'][] = 'myclass';
    $output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
  }
  else {
    // Modules may have added important attributes so they must be included
    // in the output. Additional classes may be added as array elements like
    // $variables['attributes_array']['class'][] = 'myclass';
    $output = '' . $variables['name'] . $variables['extra'] . '';
  }
  $output = '<div class="username">' . $output . '</div>';
}

Related Articles:

Neerav Mehta

Neerav Mehta

Founder & CEO

Neerav Mehta is the Founder & CEO of Red Crackle. With sterling qualities, Neerav’s technological acumen is firing a generation of progressive companies on the digital path. With an undergraduate degree in Electrical Engineering from India's most prestigious institution IIT Bombay and having spent seven years developing and contributing to the launch of AMD's innovative line of computer products, Neerav founded Red Crackle where he is lauded for his dynamic and innovative genius.

View all posts

>

Read Next

10 Tips For Entrepreneurs In 2015

10 Tips For Entrepreneurs In 2015

Learn more

10 Ways To Increase Productivity At Work

10 Ways To Increase Productivity At Work

Learn more

30 best WordPress widgets for your site

30 best WordPress widgets for your site

Learn more

Let’s get you started!

Contact Us

>

RedCrackle

Explore

About Us

Services

Contact Us

Our address

5346 Gerine Blossom Dr,

San Jose, CA 95123

USA

Socials

Twitter
LinkedIn

© 2023 RedCrackle. All rights reserved.