Using a different theme on specific Drupal pages

Disclaimer: The following post refers to Drupal 6.

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.

Comments

Kondalu1

Thanks for saving lot of time

init_theme() not a function

init_theme() is not a function. Moreover, doing this at hook_init() will not set the theme, as drupal_theme_initialize() has already run by the time hook_init() is invoked.

You want hook_custom_theme():
http://api.drupal.org/api/drupal/modules!system!system.api.php/function/...

Ah, to be fair...

... it looks like you're talking about Drupal 6, in which case this method would work.

It would be good to mark the code as such. Drupal 7 has been in full release for over a year.

Cheers!

True

This code refers to Druapl 6, not Drupal 7.
I'll add a small disclaimer to the original post.
Thanks for the comment.

Excellent article

Thank you very much for this enlightening article, in the above method I believe it would be worth to add condition where init_theme() usage with $custom_theme variable fails. A detailed account is provided here http://old.nabble.com/$custom_theme-when-does-it-take-effect...-when-does-it-not-td18990169.html . In a nutshell when some piece of code has essentially called theme() before this code is executed, very much possible despite being in hook_init() the theme is already set and init_theme() does not work the desired way.

thanks a lot

this is really helpful.thanks you.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Image CAPTCHA