Skip to content

Instantly share code, notes, and snippets.

@cecilemuller
Created March 4, 2012 14:23
Show Gist options
  • Save cecilemuller/1973201 to your computer and use it in GitHub Desktop.
Save cecilemuller/1973201 to your computer and use it in GitHub Desktop.
Programmatically change the active theme in Drupal 7
<?php
/**
* Defines a theme callback function per registered path.
*/
function MODULENAME_menu_alter(&$items) {
$items['node/%node']['theme callback'] = 'MODULENAME_default_node_theme';
$items['node/%node/edit']['theme callback'] = 'MODULENAME_edit_node_theme';
$items['node/%node/edit']['theme arguments'] = array(1);
}
/**
* Theme name callback: without parameters.
*/
function MODULENAME_default_node_theme() {
return 'garland';
}
/**
* Theme name callback: with parameters.
*/
function MODULENAME_edit_node_theme($node) {
return $node->type == 'page' ? 'seven' : MODULENAME_default_node_theme();
}
/**
* Use hook_custom_theme() if the choice of theme doesn't depend on the path.
*/
function MODULENAME_custom_theme() {
//Example: Changes the theme name depending if the user has a special role or not.
global $user;
if (in_array(variable_get('MODULENAME_special_role', 0), array_keys($user->roles))) {
return 'bartik';
}
}
?>
@deepak1980GitHub
Copy link

very nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment