I'd like to create a module that detects some properties of my web site visitors and use those to select a theme. Just as an example, to clarify my needs: suppose I have two themes set up called 'extern' in /sites/all/themes/extern and one called 'intern' in /sites/all/themes/intern. So one for external users and one for internal users. I know the IP addresses of my internal users (note that they might not be on my intranet alone). Depending if the IP address is in a predefined list, I would like my site to use the 'intern' theme and otherwise use the 'extern' theme.

In my module, what functions/hooks to I need to override, what do I need to change in the passed parameters or is there another way to go about this, like call module-based functions from template.php? Each of the theme's might have a different set over overriding template files, so my 'intern' theme might have a page.tpl.php only and my 'extern' theme might have both a page.tpl.php and a block.tpl.php or even another set of template files.

I'd like to implement this using the standard PHPtemplate engine and I do not like to fork any existing modules please.



nevets’s picture

Check out Themekey plus ThemeKey Properties.

Enable all four modules (Themekey has includes 3)

Visit "Site Configuration" > ThemeKey

Click the "Settings" tab, under "Properties" find and select "system:remote_ip", save changes

Optionally enable debugging information under the "debug" tab.

Back to "Site Configuration" > ThemeKey

Click the "Properties" tab, add IPs as needed, selecting the appropriate theme.

nevets’s picture

If you want to check a range of IPs you can enable "system:dummy" then under "Properties" make a rule for system:dummy where value "dummy" (always true) and add an additional conditions that uses regex, something like system:remote_ip~/^127\.0\.[0-9]{1,3}\.[0-9]{1,3}$/ (that will match any IP starting with 127.0)

phkemper’s picture

Thanks a million. The module showed me what I wanted to know. I now know that in my module's hook_init() function I can set the global $custom_theme variable to the name of the theme I want to show for those users. To helpe others:

* Implementation of hook_init
function mymodule_init()
  // The global variable holding the name of the custom theme to use
  global $custom_theme;

  // Do some processing to figure out the name of the theme to use
  // A very simple representation of what actually needs to be done (see ThemeKey's themekey.module code)
  $useTheme = in_array( $_SERVER['REMOTE_ADDR'], $local_ips ) ? 'intern' : 'extern';

  // Set the theme name
  $custom_theme = $use_theme;
nevets’s picture

You do realize you are setting $useTheme and using $use_theme?

phkemper’s picture

Sorry, yes, a typo. But this is just an example.

BartVB’s picture


 * Implements hook_custom_theme().
function mymodule_custom_theme() {
  global $user;
  // If the current user has a special role assigned to them, then display all
  // pages of the site (including those listed above) using the 'special_theme'
  // theme.
  if (in_array(variable_get('mymodule_special_role', 0), array_keys($user->roles))) {
    return 'special_theme';