Registering a Widgetready Area

The first step in adding a new widget-ready area to your theme is to register it with WordPress, so that it will appear in the widget administration interface.

To keep our functions.php file nice and tidy, let's create a new file in the library directory of our child theme; we'll call it widget-areas.php and include it as follows in functions.php:

chapter_06/v2/wicked/functions.php (excerpt)

include('library/widget-areas.php');

In this file, we'll put all the code required to register and display our new widget-ready area. To register it we need to call on WordPress's register_sidebar function. It accepts an array of options that define the new sidebar:

chapter_06/v2/wicked/library/widget-areas.php (excerpt)

// Register the extra Footer Aside function wicked_footer_aside() { register_sidebar(array(

'name' => 'Main Footer Aside', 'id' => 'footer-aside',

'description' => __('A widget area in the footer, above the subsidiary ^•asides.', 'thematic'),

'before_widget' => thematic_before_widget(), 'after_widget' => thematic_after_widget(), 'before_title' => thematic_before_title(), 'after_title' => thematic_after_title(), )

The array passed to register_sidebar needs to have the following values:

name

The name of the widget that shows up in the back end. The name is also used in the CSS class that I mentioned earlier. We're calling our new widget-ready area "Main Footer Aside."

This is used to set an ID for WordPress's sake, which you'll need to use when you're adding the widget-ready area to your templates. It's also used on the front end as the div element's id. We're using footer-aside here.

description

As the name suggests, this should describe the intended use or location of the sidebar. It's only shown in the back-end interface. Our description is "A widget-ready area in the footer, above the subsidiary asides." For details on that funky-looking_() code, seethe note titled Localization below.

before_widget, after_widget,before_title,and after_title

This is where you'll specify the markup that will be inserted before and after each widget and widget title. Fortunately, Thematic already has functions to generate this markup for you, which pass the content through the filters we saw earlier. If you need your aside to have different markup from the other widget-ready areas, you can change it here; for now we'll use Thematic's functions so that they'll be consistent with other widget-ready areas in the theme.

Localization

The __() function used in the above example is a translation function. When writing themes or plugins for distribution, it's best practice to run any text through this function, so that it can be easily translated by your end users. Rather than digging through your PHP files to translate your theme's text, they can simply upload a translation file that includes localized variants of every message your theme uses. When WordPress encounters the_() method, it will go looking for a translation of that message in the user's chosen language. If it finds one, it will substitute it. The extra 'thematic' parameter passed to the functions is what's called a domain; it's used in a situation where the same message might need to be translated differently in alternative contexts.

There's no pressure to fully understand WordPress localization; just remember to wrap any text in your theme's interface in the translation function, and you'll be doing your job.

For more info about how to use register_sidebar, you guessed it: check out the WordPress Codex.1

1 http://codex.wordpress.org/Function_Reference/register_sidebar

Now that the widget-ready area is registered, it will appear in the back-end interface and you can add widgets to it. However, we've yet to call on it in any of our templates, so as they stand, these widgets would never be displayed. Let's fix that now.

Was this article helpful?

0 0

Post a comment