Creating Custom Regions
All Drupal themes you download will come with a number of pre-defined regions into which you can already place blocks (normally header, left-sidebar, content, right-sidebar, footer), but it can sometimes be useful to create your own.
This can seem a little daunting at first, but it's really just a simple two-step process:
- step 1 - define the custom region in your template.php file
- step 2 - add the custom region to your page.tpl.php file
So, lets see how it works.
Step 1 - define the custom region in your template.php file
All themes should have a template.php file contained within the theme folder.
Open the template.php file in your chosen editor and locate the function titled 'function nameoftheme_regions' (where nameoftheme will be the name of the theme to which you are adding a custom region). This will probably be the first function in the file, and should look something like this:
function nameoftheme_regions() {
return array(
'header' => t('header'),
'left' => t('left sidebar'),
'content' => t('content'),
'right' => t('right sidebar'),
'footer' => t('footer')
);
}Okay, now just add your custom region so that it looks like the pre-defined regions.
For example, I'll add a custom region called 'sub header', which I'll position below the header region.
To do this I just add the following line:
'sub_header' => t('sub header'),to the function, so that I get
function nameoftheme_regions() {
return array(
'header' => t('header'),
'sub_header' => t('sub header'),
'left' => t('left sidebar'),
'content' => t('content'),
'right' => t('right sidebar'),
'footer' => t('footer')
);
}Just remember that the first part of this line must not contain any spaces. In this case I have used an underscore '_', instead of a space, between sub and header (sub_header), so I'm okay.
That's step 1 sorted, so save your template.php file and continue on with step 2.
Step 2 - add the custom region to your page.tpl.php file
So far I have just defined the custom region. Drupal knows that I want the custom region, but doesn't know where to put it. So lets sort this out.
Open your theme's page.tpl.php file in your chosen editor.
This file contains the main chunk of structure for your site's pages.
Decide where you would like your custom region to appear. In this case I'm creating a custom region called 'sub header', so it kinda makes sense to have the custom region appear beneath the header!
So, just add the following code where ever you want the region to appear (replacing sub_header with the name of your own custom region):
<div id="sub_header">
<?php print $sub_header ?>
</div>and save the file.
Technically, I actually only need to use the code:
<?php print $sub_header ?>but doing so makes it harder to manipulate the content within the region, so in this case I have placed the region within a div also called sub_header which will be more easily controlled from the style.css file.
Well that's it, your new custom region is now ready for use.
To check that it really is there, log in to your site and navigate to
Home > Administer > Site building > Blocks
You should see your custom region highlighted, and also listed as an option in the region drop-down selections for each block.





Hi, I'm Laurence and this is my Drupal blog.
Don't Make Me Think!
Pro Drupal Development
PHP Cookbook
Will You Please Be Quiet, Please?
Perfect! Exactly what I was looking for. My question is, why aren't great tutorials like this available on drupal.org? Thanks, Laurence!
Hi Drupal SEO Guy - good name ;) I'm glad the tutorial helped you out.
Join the SEO guy completely! Thank you very much Lawrence! I have beel looking for this - and here it is, in human language!
Again, my hearty thanks! God bless you man!
Thanks Alexei, I appreciate the comment :)
Laurence you are the man with the Drupal plan.
Hi Laurence,
Just thought I would drop a quick note... I installed Drupal 5.1 and worked with the default themes...
This meant I could not find a template.php file in them... at least the one I did find (in the garland theme) didn't have the "nameoftheme_regions()" function in it... So, I kept hunting until I found the "engines/phptemplate/phptemplate.engine" file. I then added the custom region to that.
Is that what is supposed to happen? This way I now have the region added to all themes that use the phptemplate engine?
Make sense? Your thoughts and wisdom would be kindly appreciate!
Owen
Hello everyone
Matias - thank you for the kind comment.
Owen - you had it right first time when you were trying to add the function to garland's template.php file. Garland doesn't have this function included so you will need to add the whole thing in yourself. To do this, just open garland's template.php file and add the entire function, as it is written above, directly after the opening php tag ('<?php'). Remember to replace the 'nameoftheme' part with the actual name of your theme - in this case 'garland'. Then just follow the other instructions to add the region to your page.tpl.php file, and you should be good to go. Let me know how it goes :)
I've been struggling with this and glad to find this page.
I don't see how the region placement is done, exactly.
MyTheme had primary links below the graphic (not at the top), and I want it to use NiceMenus.
So I guess I need to define a new region, put a Nice Menus block in it, & have the block inherit from primary links.
But how does it get the style information?
Your directions suggest creating a new div, e.g., sub_header, in page.tpl.php.
But style.css for my Theme already contains details on the div containing the existing primary links.
Is there a conflict? What to do?
Post new comment