How To Prevent Widget Titles Appearing On Your WordPress Site

The humble default WordPress Text widget is arguably one of the most flexible widgets but it has a flaw.

Give it a title for clarity in the Admin interface and it automatically gets output in your WordPress site.

In this quick article, I’ll show you how stop that automatic output and let you have your Admin clarity.

Giving your Text widgets titles adds real clarity

The description of “add arbitrary text and HTML” shortchanges this widget’s capabilities as we all know that we can also include javascript and, with a bit of tweaking, shortcodes.

What’s in each Text widget?

But the Text widget has been bugging me recently, especially when reviewing the theme builders where I often have three or four text widgets in a sidebar containing custom text and javascript for an ad server.

I wanted to give my widgets a title so I knew which was which but I didn’t necessarily want the titles being output in the public site (the default behavior).

The solution, it turns out, is dead simple and a mere three lines of code.

There are certain mantras that seem to always run true for WordPress one of which is “there’s a filter or action for that”. In this case, it’s a filter, more specifically the widget_title filter which is applied to every widget title before being output in a site’s sidebar or widget area.

Clarity in Admin, hidden to public

To have my cake and eat it then, I needed some way of identifying that the title should not be output. I decided to go with wrapping the title in square brackets – [Admin interface only].

So, by hooking into the widget_title filter, I can check to see if the title is wrapped in [ ] and if it is then I’ll return an empty string, otherwise the original title is returned and ultimately displayed in the public site.

As this filter is not applied in the Admin interface, I still get my titles.

Here’s the code as a plugin. Save this as flexible-widget-titles.php in the wp-content/plugins folder of your site, go to Plugins and activate it.

1 Response

What a nice added feature for my sites and my clients’! And thank you for making it a plugin, rather than an addition to functions.php. I will create a text widget for the basic install that says something like:
———–
Just about anything can go here: words, photos, videos…

This can have a title that shows above, or you can hide its title, as I have done here, by placing the title in [brackets].
———-
That will remind my clients (and me!) how to take advantage of it.