Friday, July 8, 2011

Changing Drupal(6.2) theme on the fly

Hi,

This probably will be among my shortest post. The most commonly used module in Drupal for handling theme's is the ThemeKey module. But if you face a requirement where you have to change the theme of the page on the fly, then this post will be helpful.

To change the theme on the fly, you just need to write two lines of code i.e.

global $custom_theme;
$custom_theme="my_theme_name";

That's it. That is all that you have to do to change the page theme in Drupal.


Explanation:


In this $custom_theme is a predefined global variable of Drupal. This tells it, in which theme the current page will open. All I am doing is overwriting the value of this global variable, thus I can change theme on the fly.

Also "my_theme_name", is the name of the theme folder which you want to activate. This may sound more of a hack to you, but this works like charm. 


The requirement I had faced was, if a user views the website and the user is from a particular country, then only that user will see a completely different theme, whereas for other users, they will have the same common theme. This was my requirement, the way you want to implement this can be limitless. So best of luck.

No comments:

Post a Comment