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.

Composite image of admin interface and sidebar
Giving your Text widgets titles adds real clarity

Featured Plugin - WordPress Q&A Site Plugin

It's now incredibly easy to start your own Q&A site using nothing more than WordPress - The Q&A plugin simply and brilliantly transforms any site, or page, into a perfect support or Q&A environment.
Find out more

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.

Screenshot of the Primary Sidebar with 3 text widget areas
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.

Screen shot of same sidebar with titles on Text widgets
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
/**
* @package Flexible_Widget_Titles
* @version 1.0
*/
/*
Plugin Name: Flexible Widget Titles
Plugin URI: http://premium.wpmudev.org/blog
Description: Wrap a widget title in [] to prevent it being displayed on your website
Author: Chris Knowles
Version: 1.0
Author URI: http://premium.wpmudev.org/blog
*/

function flexible_widget_titles( $widget_title ) {

// get rid of any leading and trailing spaces

$title = trim( $widget_title );

// check the first and last character, if [ and ] set the title to empty

if ( $title[0] == '[' && $title[strlen($title) - 1] == ']' ) $title = '';

return $title;

}

add_filter( 'widget_title', 'flexible_widget_titles' );

?>

This has certainly made managing multiple Text widgets a lot easier for me. Hopefully you might find it useful too.

Featured Plugin - WordPress Pop-Up Chat Plugin

No javascript required, no third part chat engine, just fully featured chat right in your own database on your own WP sites - couldn't be easier.
Find out more

Comments (1)

  1. 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.

Participate