Working with WordPress Text Widgets

By | July 24, 2012

This article provides useful ‘how to’ advice for working with WordPress Text Widgets.

A WordPress ‘Text Widget’ isn’t just for when you want to show plain text within a website sidebar, but also can be used to run code such as HTML or PHP. However often problems some arise, such as the styling of the code / text not fitting with a website. So this article shows not just the possibilities of what is possible with a ‘text widget’, but also shows you how to handle the code under various circumstances.

Let’s start with the basics.

Okay let’s get the most obvious use for a text widget out the way, which is to have a short message with a simple html link. You can easily achieve this by writing your text, and then to create the link you use the following example: <a href=”http://www.theurl.com/page/>the anchor text</a>

Styling the widget.

There are a couple of ways of styling the widget, but the best way is to create a div layer and then to place the CSS code in the website theme’s style.css file.

Example:

Text Widget Code

<div id=”thecode”>Hello World! Here is a<a href=”http://www.link.com/page/> link</a>.</div>

Style.css

#thecode { the css code }

Control which page(s) display the widget.

Now you might not want the text widget displayed on all pages. Many advertisers for example are keen for an advert to only to be displayed on the home page, to not just display the advert, but also to only have the link on one page.

You can control which page a text widget is displayed, by using a the ‘Widget Logic’ plugin. Then once you have installed and activated the plugin, you can add new code to each widget.

So to make a text widget only appear on the homepage, the code can be is_home() .

Calling off-site code to provide a feature.

Often a text widget can be used to reduce the need to install additional plugins, such as displaying an advert. So if you are planning on adding advert to your WordPress sidebar, instead of using a plugin, you could consider using a text widget instead. Though for those who full under EU law, please make sure you comply with the new EU cookie law.

Conclusion

I hope that I’ve opened up the possibilities of how you can use a text widget, and I’m sure there are many ways, I’ve not described here. Text widgets are incredibly flexible and can be used for a variety of uses.

About the Author: David enjoys writing about WordPress text widget settings, amongst other topics, when not drinking too much tea.