I encountered an issue within Drupal with regards to admin and frontend themes. Specifically I needed to ensure that when a user creates or edits a particular type of content, it would remain in the frontend theme, and if they created or edited a different type, it would show in the admin theme.
A bit of background may give a better understanding of why I needed to achieve this.
Our site has a different theme for the frontend of the site and the backend of the site, specifically Garland and our own bespoke theme. The site features a forum, and whenever a user creates a post, by default they are taken to the admin Garland theme. Because admin users and authenticated site users can both add posts, it would be a bit of a jump for the user to be taken out of the look and feel of the frontend site, and be taken to the admin Garland theme. I believe it would cause confusion and compromise useability.
Therefore, for a forum post, and only a forum post, we required that the user be kept in the frontend theme. For all other content types, which could only be added by admin users, the Garland theme would be used.
After some searching, the way to a achieve this was by using the hooks_init function.
- You need to create a custom module so that you can implement the function. Details of this can be found on the Drupal site.
- Override the hooks_init function:
function MYMODULE_hooks_init() { }
- Within the function, use the global $custom_theme to set the current theme:
global $custom_theme; $custom_theme = "YOURTHEME";
- To make this work according to your requirements, this will need to be conditionally set depending on, for example, the current page you are on. In my case, if the user was adding a forum post, the theme would be the frontend theme.
if( arg(0) == 'node' && (arg(1) == 'add' || arg(1) == 'edit') && arg(2) == 'forum') { // theme code }
Therefore the whole function should look like:
function random_hooks_init() { if(arg(0) == 'node' && (arg(1) == 'add' || arg(1) == 'edit') && arg(2) == 'forum') { global $custom_theme; $custom_theme = "ssw"; } }