In this tutorial, you will learn how to create a drag-and-drop-able sidebar text widget that can be styled endlessly. I’m exaggerating about the endlessly portion, of course, but not much. This tutorial will also allow you to color individual sidebar titles and their backgrounds – unlike the default wordpress text widget. You will also learn how to insert images to your text widget and style it. This is part II in the Widget Styling Series. You can find Part I here.
Lets roll.
If blog content is peanut butter then your sidebar is the jelly. But I see jelly gone bad. Eww.
If you’re writing for an audience then your blog content is usually all text. Sure, you’ll throw some images in here and there, but lets agree that your blog content is 90% textual.
How about your sidebar?
In a very non-scientific study, I’ve spent the past few days browsing through some Thesis-based themes built by average people (I mean normal people with limited css experience – definitely not web developers or programmers) and I’ve noticed a pattern – like abstract paintings, people just throw text all over their sidebars. If I am your reader, where am I supposed to focus?
Many bloggers include the following in their sidebars:
- Their Twitter Feed
- Their Categories
- A blogroll or other links
- Their Archives
- And a Tag Cloud
If you have a personal blog just for fun, maybe it’s no big deal to have a bad layout with bits of text streaming down your page like a muddled waterfall of font. But if you’re trying to get people to buy your product or sign up for your ebook or newsletter, it’s a big problem if your audience doesn’t know what you can offer them.
Text, text, text, text, text.
In my opinion, with all of those words in your sidebar displayed to your reader in the exact same format, they are bound to miss the message that you really want to get across since it is sandwiched above or below your last 5 tweets, which everyone ignores, by the way.
Take a look at some default WP/Thesis sidebar text widgets used in a straight-out-of-the-box Thesis Theme:

Oh you’re still here? Sorry, I drifted off from looking at all that text.
Highlight the Important Stuff
Make it easy, like, super easy, like SUPER-DUPER obvious where you want your reader to look.
Can you spot what I want the reader to focus on in this next picture?

Sure you can, it’s obviously the Pacific Cruise Line text. And there are a few reasons why: the font of the title is much larger than the default, there’s color, there’s a border highlighting the content, and the borders are rounded and sexy. All of those elements will tell the reader LOOK HERE, HOMBRE! And that’s exactly what they’ll do.
Alright, rant = off.
Want to make your own customized text/video/signup form/flash player widget that is guaranteed to draw your readers towards it? Copy and paste from the lines below and make it happen!
Ok, begin tutorial now.
Step 1. Open up your custom_functions.php and custom.css files.
Custom What!?
Not used to editing your custom_functions.php and custom.css files? That’s OK. Here is a tutorial to get your going.
In custom_functions.php add this:
/* My Custom Widget */
add_action('widgets_init', 'my_text_widget_init');
function my_text_widget_init() {
register_sidebar_widget('My Custom Widget', 'my_text_widget');
}
function my_text_widget() { ?>
<li class="widget">
<div class="my_widget_header">
<h3><span style="color: #ff0000;">Your Custom Header</span></h3>
</div>
<div id="widget_content">
<img class="widget_icon" src="<?php bloginfo('template_url'); ?>/custom/images/your_pic.jpg" height="100px" alt="" />
<span style="color: #ff0000;">Put Your Content Here</span>
</div>
</li>
<?php
}
You’ll need to replace whats in red with your own image and content.
Step 2. Open up your custom.css file and drop this somewhere in it:
.custom #widget_content {
background: #fff;
color: #111;
padding-left: 1em;
-moz-border-radius:5px;
-webkit-border-radius:5px ;
border-radius:5px ;
padding: 1em 0em 1em 1em;
border: 4px solid #777777;
}
.custom #widget_content .widget_icon {
float: right;
margin-right: 0.5em;}
.custom li.widget .my_widget_header {
background: #ffcc00;
-moz-border-radius:5px;
-webkit-border-radius:5px ;
border-radius:5px ;
}
.custom li.widget .my_widget_header h3 {
color: #111111;
font-size: 1.5em;
text-align: center;
}
Step 3. In your WP dashboard go to Appearance->Widgets
On the left hand side you should see a list of “Available Widgets.” Your new one should be there called “My Custom Widget.” Click “Add” and your custom widget should now appear in your widget column on the right hand side of the page. You might notice if you click “edit” that there are no options for this widget. That’s because we defined its contents in our custom_functions.php. That means that to change both the title, content text and image of the widget you’ll need to edit the custom_functions.php file that you copied and pasted above to add your own content.
That’s All, Folks
I’d love to know how this works for you! Please send me some links to your site if you decide to implement this so I can see how it turns out. Also, if you have any problems let me know via email (on my contact page), Twitter or in the comments. I respond to every single one and will be glad to walk you through the process of editing your custom_functions.php or custom.css files if you get stuck.
The Elusive Blue Text Box
Giving credit: The idea for custom styled widgets came from this thread on the Thesis Support Forums. The widget_init code I used is a modified version of that supplied by girlie in the mentioned thread.
Once again, if you want a primer on Thesis, custom_functions.php and custom.css you can fine them here and here, respectively. Also, if you don’t need such an elaborate sidebar text widget, here’s a lite version of how to style your text widget.
Update: in the comments below, Neil requested a version of this widget that would only show up on certain pages. Here is the updated custom_functions.php for that functionality:
/* My Custom Widget */
add_action('widgets_init', 'my_text_widget_init');
function my_text_widget_init() {
register_sidebar_widget('My Custom Widget', 'my_text_widget');
}
function my_text_widget() {
if (is_page('about')) {
?>
<li class="widget">
<div class="my_widget_header">
<h3>Your Custom Widget Header</h3>
</div>
<div id="widget_content">
<img class="widget_icon" src="<?php bloginfo('template_url'); ?>/custom/images/your_pic.jpg" height="100px" alt="" />
Put Your Content Here
</div>
</li>
<?php
}
}
Adding the above to your custom_functions.php file will cause the widget to only show up on your ‘about’ page. Change ‘about’ to whatever page you want to make it work for you.

If you want to find me on Twitter, I'm 
{ 4 trackbacks }
{ 39 comments }
I have two sidebars.
If I want to put this on my sidebar 1 (left), would I have to specify it in my PHP file?
/* My Custom Widget */
add_action(’widgets_init’, ‘my_text_widget_init’);
function my_text_widget_init() {
register_sidebar_widget(’My Custom Widget’, ‘my_text_widget’);
}
I don’t get how the sidebar 1 works…
As long as you have made more than one widget you can have one in each sidebar. To duplicate the widget I described, just copy the php and rename ‘my_text_widget’ to something like ‘my_other_text_widget’. Regardless, to put a widget in a sidebar, go to the widgets page and look in the upper right hand corner for a dropdown menu that has sidebar 1 and sidebar 2. Select your desired sidebar, click “show” and the active widgets for that sidebar will appear. Then just click “add” and your custom widget should hop over there.
This was super. It worked Makes for such a different look in the widget area.
Thanks much.
Awesome! I’m glad that you’re able to get some use out of it. It feels great knowing that you’re about to use it.
Great tutorial, thanks
It’s a long shot but do you think there is any way to add a conditional argument to make the custom widget only appear on a particular page?
Hey Neil, thanks for the kudos. Sure, there should definitely be a way to make it only appear on one page using
if is_page('whatever'). However, I’m not sure if we would lose drag=and-drop capability in doing so since I haven’t tried it. Worst case scenario is we would have just to hard code it in to custom_functions.php using a conditional statement, which is no biggie. Let me know if this is something you’re really interested in and I’ll put something together later this weekend.Thanks R.J., I would love to see it. I just found a plugin which allows you to choose which widgets to display only on particular pages but it might be better to do this without the weight of a plugin. Do you think that’s right or would a plugin solution be best?
Hey Neil, I’m a big fan of plugins, especially when they help me accomplish something complicated in an easy way. At the same time, plugins on a wordpress site are a lot like Motley Crue on the Sunset Strip in the 80s – too much indulgence will make them overdose.
Maybe that’s a horrible analogy
What I mean is that if your site is pulling data from a lot of plugins, it can negatively affect site load time and response. You probably already know all of that stuff. So if you’re running, say, 10 or fewer plugins then it’s probably ok to add another, but if you are concerned about load time and you can accomplish a task without using a plugin then that’s a better call to make. Having said all that, I don’t know why I thought having a custom widget display on certain pages would be a big deal – I just added a line of code and now you can use your custom widget on as many or few pages or posts as you want. Here’s the updated code for page/post specificity:
/* My Custom Widget */add_action('widgets_init', 'my_text_widget_init');
function my_text_widget_init() {
register_sidebar_widget('My Custom Widget', 'my_text_widget');
}
function my_text_widget() {
if (is_page('about')) {
?>
Pacific Cruise Line
<img class="widget_icon" src="/custom/images/calendar_128.png" height="100px" alt="Coffee?" />
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mollis, turpis suscipit sodales congue, magna urna egestas erat, vitae pulvinar tortor lorem vitae dolor. Ut sed mauris nisi.
<?php
}
}
Adding the above into your custom_functions.php folder will cause the widget to only appear on your ‘about’ page. You can change that to whatever page you want as long as you use the slug (the lowercase version of your page title). Also, if you want it on single posts instead of pages, you would replace if
(is_page('about'))with if(is_single('whatever')). A better explanation can be found in the wp codex. If you copy and paste this code, make sure to watch out for the commas turning into curly commas in the visual editor! WP will then change their html and render the code useless.Well that looks fantastic R.J. Thank you very much for your work on this and for sharing it. Much appreciated!
Thanks man I am going to use this on my thesis theme. I too have learned that too much text in the side bar is lame. I can’t believe how long I have been doing that. On my new site, I’m learning from you guys like crazy. Time to make some money and cut the BS.
Thanks Sean! Let me know when you put up the custom widget in your sidebar – I love seeing what people do to their sites. Sometimes I don’t even notice that they used my tutorial
I am officially a leech! I am poaching all of your sh*t! To assuage my guilt, I’m going to post anything I develop on the forums…. This worked beautifully for me… haven’t styled it yet.. just getting the functionality worked out on my site first.. Thanks Rob…
For my purposes, I will need to be able to specify multiple categories for this… I managed to just change “page” to “category”… but what if i need to specify say – 10 categories? what is the correct syntax… hopefully your answer will help everyone!
or/// should i just be using Widget Logic and a text widget [which aren't showing up for me for some reason...]
—K
I Just figured it out by checking the WP Codex.. heres what you do:
function my_text_widget() {
if (array(9,’category1′,’category2′, ‘category3′)) {
?>
Note: the nine represents the category number, so you can also list them that way. Seems like you can use the Category “name” “slug” or number and it will work.
works for me so far….
Kelcey..
Hi RJ – This is a super valuable post…thanks a lot.
I’ve implemented the code, made the changes in red and the widget appears with a picture, but the borders and colors don’t appear?
Hey Matthew, the borders are defined by the #widget_content id in your custom.css. If they’re not showing up, maybe it’s being overridden by some other part of your css further down the sheet? Shoot me an email if you are still having trouble.
Great tutorial, really stoked I found your site. I implemented the code on my site, tweaked it a bit to match the look of my site and it works great.
One weird issue (not related to your code) I’m having with widgets is that there’s no padding above the top widget in the sidebar. Right now I have an empty “text” widget in there to provide some padding, but that’s a hack, not a solution.
If you see an obvious fix I’d love to hear it. I probably did something weird when I was customizing the look of the site, so if you can’t figure this one out no worries.
Hey man, thanks for the kudos!
I checked out your site (great logo btw!) and looked at your sidebar to see if I could find any rogue code that was messing up your design. I didn’t see anything, but I think you should be able to give yourself some padding there using css instead of a text widget. Try something like
.custom #sidebar_1 { margin-top: 2em; }and see if that gives you some extra space at the top. For some reason, I have the opposite problem (too much white space) when I customize sidebar_1 on a 3-column layout.R.J. Thanks a ton, that worked perfectly. I also used the conditional display code you post earlier in this thread. Everything is looking great.
I got the logo from Soner Ertim. He did great work for a very affordable price, I’d recommend him to anyone.
Thanks again,
Esoos
Love it! I’m a cut and paste wizard. Don’t know a lick of css nor php and I just rip off-I mean utilize-bits and pieces from all over the place to get my own unique thingies.
I like to use the open-hook plugin (and I want to be consistent after all
…so what hook would I use? And how would I amend the code that goes right into the custom_functions.php file that you provided?
Thanks
Hey Tobi, I am happy to supply your copy and paste habit. I’ll be your dealer any day.
I love openhook, but I’m going to share a secret with you that will make it easier to use for this particular widget. Just click on the tab that says “Thesis Custom Programming” near openhook and paste the code in there. This is assuming that it’s linked up with your custom files of course, but for like 99% of the people it’s set automatically I believe. If that doesn’t work please let me know and I’ll do my best to give you a more usable answer.
Worked just fine!
lol-I thought that “thesis Custom programming” was open-hook’s way of dealing with custom_functions.php…you mean THAT isn’t open hook too?
Now I’ve just got to style the widget, and fix the spacing/padding…
Thanks again.
Tobi
PS. I said “wizard” not “junkie”…I have my standards you know
Hey R.J.,
Just thought of another question.
What if we want to do this, use this custom widget, in more than one place?
Since we can’t style it in the widgets area, do we have to create custom widget2, custom widget3 etc? i.e effectively create a new widget each time we want to do this…
Damn, now I have to actually think instead of cutting and pasting if that’s the case….
Tobi
Hey!
Guess who? Yeah another dumb question…you’d think I’d think of all of these at once…
What if I want to make the widget “clickable”…or at least a portion of it…to get the reader to click through to a page or a post?
How do I do that huh?
Tobi
You can make any portion of text or image clickable by adding Sample Text or Image to it. Also, if you want to make multiple widgets then you’d need to copy the code and change the names as I’ve described earlier on in the comments. After you do it once you’ll get the hang of it and it will be a breeze to make multiples in the future.
Hi Rob – I never did get a chance to thank you for your help a few weeks ago. Your help was invaluable. I included the interaction in a post today: http://mattscottnelson.com/2009/07/25/social-media-marketing-whats-the-goal/
Thanks again,
Matt
Phew…getting there!
So I’ve created another sidebar, and want to add a second custom widget.
I’ve basically done what you said above “rename ‘my_text_widget’ to something like ‘my_other_text_widget’”
And now my code looks like this.
/* My Custom Widget2 */
add_action(’widgets_init’, ‘my_2_text_widget_init’);
function my_2_text_widget_init() {
register_sidebar_widget(’My Custom 2 Widget’, ‘my_2_text_widget’);
}
function my_2_text_widget() { ?>
Pacific Cruise Line
<img class="widget_icon" src="/custom/images/your_pic.jpg” height=”100px” alt=”Coffee?” />
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mollis, turpis suscipit sodales congue, magna urna egestas erat, vitae pulvinar tortor lorem vitae dolor. Ut sed mauris nisi.
<?php
}
/* My Custom Widget2 End */
Now comes the styling question…I haven't changed any classes/id's so when I style the first widget, I think it will impact this one.
Would you mind showing the proper naming syntax if I want to create classes for this second widget? (and where to do it…widget? widget_content? all the above?)
Perhaps you could just cut and paste my code above and insert the corrections with a colored font?
Thanks a ton.
Tobi
Hey Tobi, so far so good. To change the style/css for each widget all you’ll need to alter are the divs. That means rename “my_widget_header” and “widget_content” to “my_widget_header2″ and so on. Extrapolate that to the css that you’re already using and you should be good to go. If that doesn’t make sense let me know.
Wow Matt, that’s incredibly generous of you! Thank you so much, that really means a lot
R.J.
You said above, “For some reason, I have the opposite problem (too much white space) when I customize sidebar_1 on a 3-column layout.”
Me too. How do I solve that?
And 2, how would you reduce the width of the widget/sidebar?
You can see sidebar #3 on my http://www.weblegalmarketing.com site. It’s where the phone number is…how do you “indent” that blue box on both the left and right?
Thanks
Tobi
@Tobi, to answer question 1, I usually use margin-top on sidebar two to slide it down to match sidebar 1. As for your other question: in your widget_content div, try adding margin-left: 5px; and width: 90%;. Tweak the numbers to your liking.
Hi,
Great tutorial! really clear and great help.
Couple of quick questions for you if you don’t mind:
1) I have inserted a background image for my widget and looks good but have noticed the widget overall is slightly positioned more to the left, anyway of moving it central?
2) I would like to colour my a:hover,visited,etc links just for the widget, how would I go about this?
Thanks in advance.
Hey Tom, can you post a link to an example? It will help with the background positioning.
R.J., I used your widget styling before with no problem.
This time, I pasted the code in custom functions and screwed something up royally.
After pasting, I can no longer log into wp-admin. I can access my site okay. The only thing that looks weird is the sidebar text has jumped down to the bottom of the bar.
This is the error message I get:
Parse error: syntax error, unexpected ‘<' in /home/content/l/l/o/lloydcw333/html/listlocal/wp-content/themes/thesis_15/thesis_15/custom/custom_functions.php on line 36.
Don't know what Line 36 is. And I'm dead w/o being able to log in.
I have tried uploading my old custom functions file, tried uploading the original custom functions file. But I get nowhere.
I was about to send a post to the forum, but thought I would try here 1st since this is what I was working on.
Do you have any idea what could have happened?
Thanks R.J.
Carol
Hey,
Its actually an internal site which I am creating for a Company Intranet.
I have solved both though, thanks for replying though.
Tom
Hey Carol, I have updated the code since you last used it. Did you try to highlight it and copy that way or did you click the “copy to clipboard” button? If you did the former, it won’t work since you’ll have all those extra line numbers. If you did the latter it should work fine.
Thanks RJ. My computer was just drunk that day. I did not think that this post I sent even transmitted. I tried twice to send it and the page would just hang up.
No, I did not past the code with the line numbers. I knew that would look goofy sitting in the file.
I gave up on working with for a bit RJ. Then I went back and tried replacing the custom_functions php file one more time – and it worked. I have my admin panel back.
I need to stop and take a course cuz it’s costing me anyhow with the time I waste trying to figure stuff out.
I agree with the others. You provide great info.
@Tobi.
Tobi, I like your legal marketing blog. Great opt-in box. Wish you well with it.
Carol
Thanks for the tut R.J. This saved me hours of aggravation, heartache, depression, gnashing of teeth, drooling profusely, and yelling at the neighbor kids. Thank you, thank you!
Comments on this entry are closed.