How to force a different theme for specific Admin pages when the Administration theme is set (eg. node/add or node/edit)

Here is a quick tip on how to use the default website theme (or any other theme) instead of the administration theme when adding a specific Content Type, eg. http://website.com.au/node/add/widget

This is useful if you want the Administration Theme to be displayed for all 'admin' pages. But have a few Content Types that users submit on the website which must use the website theme.

To do this you create a custom module and insert the following function.

1. Create the widget.module and the widget.info file
2. In the widget.module insert the following code.
3. Change the args according to the path you wish to check for. The code will switch the theme for node/add/widget
4. This line

<?php
$custom_theme
= variable_get('theme_default', '0');
?>
will get the default theme for the website. However you can just as easily insert the name of the theme you wish to apply.

<?php
/**
* Implementation of hook_menu(). Force loading of the default theme instead of the Administration theme
*/
function widget_menu($may_cache) {
  if (
arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'widget') {
    global
$custom_theme;
   
$custom_theme = variable_get('theme_default', '0');
   
init_theme();
  }
}
?>

Useful references:
http://drupal.org/node/242200
http://drupal.org/node/282971
http://api.drupal.org/api/function/page_example_menu/5
http://api.drupal.org/api/file/developer/examples/page_example.module/5

Updated Version for Drupal 6

JuliaKM - February 25, 2009 - 15:11

I ran into the same problem and wrote a small module based on this one. I wanted the default theme to only show up on the edit pages of the wiki content type I created. Here's what I put in mymodule_theme_switcher.module:

<?php
function mymodule_theme_switcher_init() {
    global
$custom_theme;

 
$content_type = my_node_type_load(arg(1));

  if (
arg(0) == 'node' && arg(2) == 'edit' && $content_type->type == 'wiki') {
   
$custom_theme = variable_get('theme_default', '0');
   
init_theme();
  }
}

function
my_node_type_load($arg) {
 
$node = node_load($arg);
//checking for wiki node type, the only time that I want to use my default theme and not the admin one
 
if($node->type == 'wiki')
    return
$node;
  return
FALSE;
}
?>

The code is working for me right now but I'm sure there's a better way to do this.

Drupal 6 Version - Add & Edit

intrafusion - June 5, 2009 - 10:19

Thanks JuliaKM for your snippet, I have changed it for add & edit for 2 different content types

<?php
function theme_switcher_init() {
    global
$custom_theme;
   
   
$content_type = my_node_type_load(arg(1));
   
    if (
arg(0) == 'node' && ((arg(1) == 'add' && (arg(2) == 'market' || arg(2) == 'directory')) || (arg(2) == 'edit' && ($content_type->type == 'market' || $content_type->type == 'directory')))) {
       
$custom_theme = variable_get('theme_default', '0');
       
init_theme();
    }
}

function
my_node_type_load($arg) {
   
$node = node_load($arg);
   
//checking for market or directory node type, the only time that I want to use my default theme and not the admin one
   
if($node->type == 'market' || $node->type == 'directory')
        return
$node;
    return
FALSE;
}
?>

Some warning here

zvischutz - August 21, 2009 - 15:36

I hope I am not saying the obvious but some warning here:
I was having trouble with this one and while debugging I put some watchdog calls here. It turns out the watchdog calls initialized the $theme variable and than the init_theme() did nothing ...
So everything was working but I messed it with the watchdog calls , how annoying !

 
 

Drupal is a registered trademark of Dries Buytaert.