Enable Widgets in a WordPress Theme

I’m still surprised at the lackluster search results I get when I try to remember how to enable widgets into a WordPress theme. In this tutorial I’m only going to go through the basics, nothing fancy at all… just dry conditional statements. In general this requires you to edit or create two files:
- /wp-content/themes/{theme of the day}/functions.php file.
- /wp-content/themes/{theme of the day}/sidebar.php file.
Theme Functions
This file (functions.php) can be a pretty powerful addition to your WordPress theme. Basically; you can put all your PHP functions into this file and call them at-will on other pages. In this case all you need to do is add a function to “register” the sidebar feature.
<?php
if (function_exists(‘register_sidebar’))
register_sidebar();
?>
You can simply use “register_sidebar()”, but the conditional statement is simply a best practice for coding just in case for some odd reason that function isn’t available. That way your site won’t throw an error and disrupt the rendering of your beautiful WP theme.
Applying the Sidebar
Now that you’ve told your WP theme to use the sidebar feature, you’ll need to actually apply it. Usually this next function is applied to the sidebar.php file. Edit it to run the following code:
<?php
if(function_exists(‘dynamic_sidebar’))
dynamic_sidebar();
?>
…and you’re done! Next time you log into /wp-admin and click on Appearance | Widgets you’ll see options of attaching widgets to your sidebar! You’re welcome!
Pingback: wordpress