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.

// 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.

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.