Community Documentation

Changing theme based on http headers

Last updated August 25, 2009. Created by m3avrck on June 14, 2006.
Edited by lisarex, jhodgdon, bekasu, sepeck. Log in to edit this page.

A simple way to alter your theme based on HTTP headers can be accomplished using the following code:

<?php
  $headers
= drupal_set_header();
  if (
strstr($headers, 'HTTP/1.0 404 Not Found')) {
   
// do something, output something, change template file, etc...
 
}
?>

Works great and works with all other headers. Great for having a completely different layout/theme for 404 pages.

Changing Theme with Init Hook

If you need to actually change a theme, you need to write the following in a module's init hook:

function yourmodule_init() {
   global $custom_theme, $theme;
  
   //... some logic ...

   $theme = null;
   $custom_theme = 'chameleon'; // 'chameleon' being the theme you want to set
  
}

About this page

Audience
Developers and coders, Site administrators, Themers

Theming Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here