As long as your WordPress theme is widget-ready, you can edit the content of your sidebar through the dashboard. In the Design > Widgets panel you can add widgets to, remove widgets from, and reorder widgets within your sidebar.

The default selection of widgets is pretty good, and there are plenty of plugins out there that will add new widgets for you to use, but what if you can’t find a widget that does what you want? Does that mean that you need to abandon your dynamic sidebar and go back to hard-coding your sidebar.php theme file?

Not at all. Instead, you can create your own sidebar widget. That way, you get the flexibility to put exactly the code that you want in your sidebar, but you can still change what sidebar items go where through the dashboard.

Let’s suppose that we want to create an author info section in our sidebar. The HTML for this might look something like this:

<li id="author-info" class="sidebaritem">
  <h2>Author Name</h2>
  <p><img src="<?php bloginfo('template_directory'); ?>/images/author.jpg" alt="Author Name" /></p>
  <p>A catchy summary of who you are and what make you so special</p>
</li>

To turn this into a widget, we just need to do three things to our functions.php theme file.

First, we need to tell WordPress that when it loads a list of sidebar widgets it should try to load our Author Info widget:

add_action('widgets_init', 'author_info_init');

When WordPress loads a list of widgets, it checks to see whether any functions have been hooked to the widgets_init action. This code hooks the author_info_init function to widgets_init, telling WordPress that when it loads a list of widgets it should call this function.

Next, we need to define the author_info_init function to register our Author Info widget so that it can appear in the list:

function author_info_init() {
  register_sidebar_widget('Author Info', 'author_info');
}

This registers the widget, naming it “Author Info” and associating it with the author_info function. The author_info function will now be called wherever our widget appears on our website.

Finally, we need to create the content of the widget by defining the author_info function so that it outputs the HTML that we want:

function author_info() { ?>
  <li id="author-info" class="sidebaritem">
    <h2>Author Name</h2>
    <p><img src="<?php bloginfo('template_directory'); ?>/images/author.jpg" alt="Author Name" /></p>
    <p>A funky description of who you are and what make you so special</p>
  </li><?php
}

Once we’ve saved and uploaded our new functions.php file, we can go to the Design > Widgets panel in our dashboard, add our Author Info widget to our sidebar, and drag-and-drop it into whatever position we want.

Of course, any code can be turned into a widget in this way, so the lack of a pre-existing widget that gives you the code that you want is never a reason to abandon your dynamic sidebar.

WordPress Image Rotation

Website owners often want to add interest to their website by showing a different header image each time a page is loaded. The WordPress codex includes instructions for implementing header image rotation on your WordPress powered site, recommending a script by Matt Mullenweg.

The script works very well. It’s easy to implement, and to add or remove images to or from the rotation, you just need to upload them to or delete them from a specific folder on your server. Once you’ve uploaded the script and some images for it to use, you call the image using the following code in your theme files:

<img src="<?php bloginfo('template_directory'); ?>/imagedirectory/rotate.php" alt="" />

The only problem is that in FireFox it doesn’t work. FireFox caches content aggressively, and it will run the rotation script, cache the result, and keep showing the same header image over and over again.

To prevent this from happening, we need to convince FireFox that the rotation script isn’t the kind of thing it should cache. To do this, we can pass a randomly generated variable to the script to make it look dynamic:

<img src="<?php bloginfo('template_directory'); ?>/imagedirectory/rotate.php?fakequerystring=<?php echo rand(0,1000); ?>" alt="" />

The rotation script won’t do anything with the variable, but FireFox isn’t to know that, so every time you pass a different variable to the script FireFox will run it again rather than loading a cached image.