Need some help with your project? Contact me

How to add a new region in your page template in Drupal 7

So you have your Drupal 7 theme and you want to add blocks also in places your theme does not have regions assigned for blocks. Provided you know a little .html and .css, and the sight of .php does not scare you, adding new regions to your theme is quite simple.

Step 1:

Go to your theme.info file (You'll find that in your theme root directory. For Bartik, this would be bartik.info).

There you'll find, among other things, something like this, i.e. theme regions being declared:

regions[header] = Header
regions[help] = Help
regions[page_top] = Page top
regions[page_bottom] = Page bottom
regions[highlighted] = Highlighted

You'll need to add a new line with the name of your new region like so:

regions[machine_name_of_your_region] = Name of your region

Save and exist.

Step 2:

Although you can also call region blocks in the node template as well, you will generally add the region to the theme's page template. This is usually page.tpl.php, or if you have custom template files for different pages, edit those ones too. The new region will appear only on the pages built around the template you add the new region to.

OK, so there you'll find some .html arranging some dynamic content given by Drupal with .php. What you need to do is add your newly declared region. What I did, and i also think is pretty simple to do, is to copy a preexistent region and just change the .php variable to the one of your new region declared in the theme.info file.

In the Bartik theme for instance, you can copy this:

<?php if ($page['sidebar_first']): ?>
<div id="sidebar-first" class="column sidebar"><div class="section">
  <?php print render($page['sidebar_first']); ?>
</div></div> <!-- /.section, /#sidebar-first -->
<?php endif; ?>

And paste it wherever you want your new region to appear. After you paste it, change the old variable (in this case sidebar_first) to the new variable (in our case machine_name_of_your_region).  Like so:

<?php if ($page['machine_name_of_your_region']): ?>
<div id="sidebar-first" class="column sidebar"><div class="section">
  <?php print render($page['machine_name_of_your_region']); ?>
</div></div> <!-- /.section, /#sidebar-first -->
<?php endif; ?>

Although not the subject of this post, you'll have to change also the DIV IDs and classes to however you want to style and place the new region.

After you made these changes and you are satisfied with where you placed the new region, save the file. Go to your Drupal website, to Appearance and just as if you had changed something to the configuration of your theme, give it a Save to refresh itself. Would probably not hurt also to clear your website's caches (Configuration - Performance - Clear all caches).

Now you should see your new region in the Blocks section and if you add a block to it, it will hopefully appear where you added it in your Page template.

Hope this helps!

NB: A good way to later manage your block display in regions is using the Context module.

Comments

great i liike this

This is very useful post for me and it helps me a lot. Thank you so much for this and have a great time.

Add new comment

You can post comments in Markdown and basic HTML tags.
For code blocks, wrap your code within '~~~'. For example:
~~~
$var = 'my variable';
~~~