Drupal (6 & 7): programmatically change the active theme


/ Published in: PHP
Save to your folder(s)

**Important: this snipplet has moved to Github.**

- [Programmatically change the active theme in Drupal 6](https://gist.github.com/1973197)
- [Programmatically change the active theme in Drupal 7](https://gist.github.com/1973201)


Copy this code and paste it in your HTML
  1. // DRUPAL 6
  2. function mymodule_init(){
  3. global $custom_theme;
  4. $custom_theme = 'garland';
  5. }
  6.  
  7.  
  8.  
  9. // DRUPAL 7
  10. /**
  11.  * Defines a theme callback function per registered path.
  12.  */
  13. function mymodule_menu_alter(&$items) {
  14. $items['node/%node']['theme callback'] = 'mymodule_default_node_theme';
  15.  
  16. $items['node/%node/edit']['theme callback'] = 'mymodule_edit_node_theme';
  17. $items['node/%node/edit']['theme arguments'] = array(1);
  18. }
  19.  
  20.  
  21. /**
  22.  * Theme name callback: without parameters.
  23.  */
  24. function mymodule_default_node_theme() {
  25. return 'garland';
  26. }
  27.  
  28.  
  29. /**
  30.  * Theme name callback: with parameters.
  31.  */
  32. function mymodule_edit_node_theme($node) {
  33. return $node->type == 'page' ? 'seven' : mymodule_default_node_theme();
  34. }
  35.  
  36.  
  37. /**
  38.  * Use hook_custom_theme() if the choice of theme doesn't depend on the path.
  39.  */
  40. function mymodule_custom_theme() {
  41.  
  42. //Example: Changes the theme name depending if the user has a special role or not.
  43. global $user;
  44. if (in_array(variable_get('mymodule_special_role', 0), array_keys($user->roles))) {
  45. return 'bartik';
  46. }
  47. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.