Add To Wordpress Sidebar.php

Wordpress Widgets are pretty cool. They take all the guess work out of adding content to your sidebars. But, what if you want to add content manually? There are many situations where someone would want to add content into their sidebar manually. Widget support can only go so far. With Widgets you can add basic HTML code, JavaScript code (Such as Google Adsense code) and quite a few other things. However, there are many things that you can’t add to your sidebar using widgets. And that calls for adding content direct to your sidebar.php file manually.
If you open up your sidebar.php file you will see some lines of code similar to this:
<div class=”sidebar_left”>
<ul>
<li>
<?php include(’adsense_sidebar.php’) ?>
</li>
<?php if ( !function_exists(’dynamic_sidebar’) || !dynamic_sidebar(1) ) : ?>
<?php endif; ?>
</ul>
</div>
The code that says this:
<?php if ( !function_exists(’dynamic_sidebar’) || !dynamic_sidebar(1) ) : ?>
<?php endif; ?>
is the code that displays your sidebar widgets found here:

If you want to add content above or below the content that gets displayed within your widgets, then simply add your content above or below this line of code:
<?php if ( !function_exists(’dynamic_sidebar’) || !dynamic_sidebar(1) ) : ?>
<?php endif; ?>
We can use my current sidebar.php as an example. On my site I like to display the names of my Top Contributors. Basically people who leave the most comments rank higher on the list. First thing I had to do was download and install the Wordpress plugin. After I installed the plugin I had to active it. Once it was activated, I needed to insert the php code into my sidebar.php file. This could not be done with the Wordpress sidebar widgets. It has to be done manually. Here is how I did it:
<li><h2>Top Contributors</h2>
<ul><?php ns_show_top_commentators(5); ?></ul>
</li>
If you look at the original code on the sidebar.php file, you will see the the sidebar begins with the <ul> code.
Adding the:
<li><h2>Title of Your Menu</h2>
<ul>Something Goes Here</ul>
</li>
will create a properly formatted list of your new manually added content on your sidebar.
Here is another example. After display the Top Contributors on my site, I then wanted to display the most popular articles on the site. Again, a plugin needed to be installed and then code needed to be added to my sidebar:
<li><h2>Most Popular Articles</h2>
<ul><?php akpc_most_popular($limit = 5); ?></ul>
</li>
Ok, Great! So lets look at the bigger picture. Lets display all three together:
<div class=”sidebar_left”>
<ul>
<li><h2>Top Contributors</h2>
<ul><?php ns_show_top_commentators(5); ?></ul>
</li><li><h2>Most Popular Articles</h2>
<ul><?php akpc_most_popular($limit = 5); ?></ul>
</li>
<li>
<?php include(’adsense_sidebar.php’) ?>
</li><?php if ( !function_exists(’dynamic_sidebar’) || !dynamic_sidebar(1) ) : ?>
<?php endif; ?>
</ul>
</div>
What about making up my own stuff? What if I want to make my own list? Perhaps it would be a neat idea to display a list of posts that I feel are important!
No problem… check this out:
<li><h2>Author’s Top Five</h2>
<ul>
<li><a href=”yourblog.com/yourposturl.html/” title=”Your Post Title #1″>Your Post Title #1</a></li>
<li><a href=”yourblog.com/yourposturl.html/” title=”Your Post Title #2″>Your Post Title #2</a></li>
<li><a href=”yourblog.com/yourposturl.html/” title=”Your Post Title #3″>Your Post Title #3</a></li>
<li><a href=”yourblog.com/yourposturl.html/” title=”Your Post Title #4″>Your Post Title #4</a></li>
<li><a href=”yourblog.com/yourposturl.html/” title=”Your Post Title #5″>Your Post Title #5</a></li>
</ul>
</li>
Just remember that your sidebar displays a very simple form of HTML that has been used for many years since the early HTML days in the early 90’s. It’s called the Un-Ordered List. And it is way easy to use. The most common problem people face it that they get overwhelmed with the countless lines of code.
I hope that this helps you when you try to add some content to your sidebar manaully. If you should have comments, questions or suggestions, please feel free to drop me a comment below.

| 3.1 |
6 Comments! Join The Discussion by Leaving Your Comment.
What do you have to say about this post? Leave a comment!


Date/Time: 7-28-2007 19:36:10 Comment #4025
But… what if I need to inser a snippet of PHP code as one of the widgets? Is it possible at all?
Date/Time: 7-28-2007 22:12:20 Comment #4028
The only theme that I have seen that allows for adding php code thru widgets is K2.
But since Wordpress has started releasing their install already Widgets ready, this created a nightmare for former K2 users… a patch was then released on K2 to disable Wordpress Widgets because it conflicted with their own K2 Widgets.
All in all I am not too big on Widgets anymore… back when I didn’t know how to write code (still don’t but I can tweak things pretty good) widgets were awesome for me. However, today, in most situations I completely remove Widgets from my themes and hard code my sidebars myself.
Adding php code to your sidebar is pretty easy, this article should help you with it.
Date/Time: 7-28-2007 23:09:48 Comment #4031
Thanks a lot. I myself don’t write code, but I can find my way in tweaking and inserting/removing stuff. And yes, I do feel better when there is no widgets, then all the code is in front of me so I can get what I want most of the time.
Date/Time: 7-30-2007 08:32:42 Comment #4050
Hi Garry,
Thanks for this great and easy to understand article.
Re PHP scripts in the sidebar widgets, Semiologic Pro allows this and it really does make life a dream. I do have a client who wanted to add flickr photos to her site using falbum and since this requires the use of php and, she isn’t using Sem Pro I wanted to find an alternative. Thanks again for the clear instructions Garry.
Date/Time: 7-30-2007 11:30:58 Comment #4051
Hey Trish,
You are welcome. I am glad that it helped. It’s great to see new faces and people. Welcome to the site!
Date/Time: 9-18-2008 05:29:32 Comment #22643
Thanks for the article Gary. I’m using K2 with widgets and I do a lot of tweaking around. I have my doubts if php would work within widgets but I’ll definitely give it a try.