You are hereUsing a different theme on specific Drupal pages

Using a different theme on specific Drupal pages


Ever wondered how to have a page, or several, with one theme and the others with another?
Usually, this comes up when you wish to have the site with one theme and the administration pages with another. That’s the simplest scenario and is solved simply by using the built in functionality in Drupal core.
Just go to Admin -> Site Configuration -> Administration Theme and select the theme you wish to use for only your administration pages.

Another situation is when you wish to have the administration theme applied not only to the administration pages (those who have the ‘admin’ in their URL), but to other pages. To do this you need the help of a module surprisingly called Administration Theme. This module allows you to apply the administration theme of your choice to pages like: batch processing pages, devel node load and render pages, coder code review pages and pages you define yourself.
Just download and install the module and on the same Admin -> Site Configuration -> Administration Theme page you’ll have some new options that allow you to master the new configurations.

So far, I didn’t point out anything new and exciting, but the post is not over…

The third scenario and the one that I have come across recently, is the one where you wish to have a different theme applied to a specific set of pages. Not all administration pages and not those and other pages. In my case, I had to use a different theme just for the editing pages that belong to the Panels module. Luckily, those pages have one thing in common and that is that their URL always starts with ‘admin/build/pages’. This allowed me to create a rather simple solution to the task in hand.
Inside my own module, I have created a function called mymodule_init(). Inside, I checked if the page’s URL currently being visited starts with ‘admin/build/pages’. If so, I use the global variable $custom_theme and set it to the other theme I wish to apply. Then all I needed to do is call init_theme() in order to load the theme.

The $custom_theme global variable allows you to override the Drupal theme programmatically and then by calling init_theme() you initialize the Drupal theme system using the theme you have set in that global variable.

Here is the code:

function mymodule_init() {
  // get global variable to override Drupal theme
  global $custom_theme;

  // check if the current page needs to have its to theme changed
  if (stripos($_GET["q"],"admin/build/pages/") !== FALSE) {
    // set the custom theme and initialize theme system
    $custom_theme = 'garland';
    init_theme();
  }
}

 

My case was probably a specific issue I came across with and probably not relevant to all, but the solution can be applied to other situations as well.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

ABOUT ME

View Ehud Ashkenazi's profile on LinkedIn

Ehud Ashkenazi is a Software Developer and Webmaster at a large eLearning company.

Working with various technologies including C-Sharp(C#), ASP 3.0, ASP.NET, PHP, T-SQL, HTML and more.

Currently holding a BA in Computer Science from the Efi Arazi School of Computer Science, IDC Herzliya, Israel.