Widgetize Your WordPress Theme
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'Widgetized Area',
'id' => 'widgetized-area',
'description' => 'This is a widgetized area.',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4>',
'after_title' => '</h4>'
));
}
This is all that is needed for the first step. Once this widgetizing code is placed in your functions.php file, proceed to Step 2. The remainder of this section is explanation.
This code checks to make sure that the current version of WordPress supports widgets, and then declares an array of values that will be used to create the widgetized area in your theme. Here are the different values:
* name – the name of the widgetized area as displayed in the WP Admin
* id – a unique identifier for your widgetized area
* description – description of the widgetized area
* before_widget – the markup generated before any user-added widgets
* after_widget – the markup generated after any user-added widgets
* before_title – the markup generated before the title of any user-added widgets
* after_title – the markup generated after the title of any user-added widgets
So, given these parameters, our widgetizing code would result in the following output, say, if the built-in Search widget were added to our widgetized area:
<div id="search-3" class="widget widget_search"> <h4>Search</h4> <form role="search" method="get" id="searchform" action="http://localhost/283/" > <div> <label class="screen-reader-text" for="s">Search for:</label> <input type="text" value="" name="s" id="s" /> <input type="submit" id="searchsubmit" value="Search" /> </div> </form> </div>
Note the markup created for the opening
'before_widget' => '<div id="%1$s" class="widget %2$s">', <div id="search-3" class="widget widget_search">
Hopefully the relationship is clear. This is a powerful way to ensure that all widgets have a similar class names and unique IDs, which are important for easy CSS styles.
Widgetize Your WordPress Theme: Step 2
Finally, add the following code to the location in your theme’s template file(s) where you would like the widgetized area to appear:
<div id="widgetized-area">
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('widgetized-area')) : else : ?>
<div class="pre-widget">
<p><strong>Widgetized Area</strong></p>
<p>This panel is active and ready for you to add some widgets via the WP Admin</p>
</div>
<?php endif; ?>
</div>