58

I want to add a new area which will be site wide. Describe the process of getting this new region into my theme, if possible generalize it for adding any new, custom region.

68

You define regions in your theme .info file. See: Structure of the .info file

You then need to put the relevant template tags in your page.tpl.php file.

So for example if you defined the region sidebar in your themes .info file, you would then add: <?php print render($page['sidebar']); ?> to the relevant place in your page.tpl.php.

  • 6
    If your theme doesn't have a page.tpl.php file you can copy the default from system module into your theme, and modify that copy. – Mikey P Mar 3 '11 at 3:56
  • 5
    Omega doesn't need the page.tpl.php part, just declare the region in the .info AND CLEAR CACHES. – Capi Etheriel Feb 25 '12 at 4:33
20

One other point: Don't miss the required regions!

regions[content] = Content
regions[help] = Help
regions[page_top] = Page top
regions[page_bottom] = Page bottom

More on this can be found at http://drupal.org/update/themes/6/7#closure.

To clarify on this point: If you define your own regions, you need to define all that you are using, including those that are required. In other words, you cannot just define additional regions: If you define any, you must define them all.

9

Creating a region for a Drupal 7 theme is not different from creating a region for a Drupal 6 region; the differences between Drupal 7 and the previous versions are essentially:

  • The regions are not defined using a mytheme_regions() function as it was done in Drupal 5, but they are defined in mytheme.info using regions[] directives, which is what is done also with Drupal 6 themes.
  • The regions are rendered using the Drupal function render(), instead of printing the content of a variable.

The default Drupal 7 regions are the following ones:

regions[header] = Header
regions[highlighted] = Highlighted
regions[help] = Help
regions[content] = Content
regions[sidebar_first] = Left sidebar
regions[sidebar_second] = Right sidebar
regions[footer] = Footer

Drupal 7 core themes now include a region named 'highlighted' which uses the same display as the mission statement area in Drupal 6; they also include a region named 'help', that by default has the same content of the $help variable used by Drupal 6.

4

The theme regions are defined in your theme .info file — see docs. Just add a line with regions[corner] = Corner to get a Corner region.

Then you need to edit the page.tpl.php file and add <php print render($corner); ?> where you want the region to show up. If your theme doesn't have a page.tpl.php, copy from the base theme or from the system module.

Common pitfalls:

  • If the theme.info file had no regions declared, it was using Drupal default regions. If you want to add, you will have to re-declare the default regions. You can, of course, remove any region but please support the required regions. See the docs again.

  • Changes to the .info file or adding new files to the theme (like page.tpl.php) will only be noticed after you clear caches.

  • Omega, a very popular base theme, won't need any change in page.tpl.php, just the .info file. But you will have to visit the theme settings in the UI and assign your region to a zone.

  • This is a wrap up of the answers with common pitfalls. – Capi Etheriel Feb 25 '12 at 17:08
  • Is regions[content] = Content the only required region in D7? drupal.org/node/171205#regions seems to imply this, but then drupal.org/node/171224 says that the required regions are content, help, page_top and page_bottom – Jim Garrison Dec 5 '14 at 19:54
  • I don't know if Drupal checks for any of the regions, but content is definitely very important. help is supported by core (to output help messages), page_bottom is probably where bottom js is printed and page_top seems to be used for navbars such as core toolbar's. – Capi Etheriel Dec 8 '14 at 18:34
2

Follow the below steps to create custom region in your theme:

  1. Go to your theme.info file and add this line.

    regions[machine_name_of_your_region] = Name of your region
    
  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. Paste it wherever you want your new region to appear.

    <?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; ?>
    
  3. Configuration - Performance - Clear all caches

0

Create a New region in Theme

You Just need to define it in two files.

  • Theme.info
  • Page.tpl.php

Theme.info

;;;;;;;;;;;;;;;;;;;;;
;; Regions
;;;;;;;;;;;;;;;;;;;;;

regions[navigation]      = 'Navigation'
regions[header]          = 'Top Bar'
regions[highlighted]     = 'Highlighted'
regions[new_region_name] = 'Region Name' ;; Add New Region

Page.tpl.php

Now render this region

  <?php if ($page['new_region']): ?>
      <div class="new_region_class">
        <?php print render($page['new_region']); ?>
     </div>
  <?php endif; ?>

Now Clear all cache, Here you go, you have a new region in your theme. You can see it at your block configuration page.

For more visit http://drupal.org/node/171205

Not the answer you're looking for? Browse other questions tagged or ask your own question.