{"id":123878,"date":"2013-12-12T11:30:58","date_gmt":"2013-12-12T16:30:58","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=123878"},"modified":"2013-12-11T22:21:49","modified_gmt":"2013-12-12T03:21:49","slug":"how-to-prevent-widget-titles-appearing-on-your-wordpress-site","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/how-to-prevent-widget-titles-appearing-on-your-wordpress-site\/","title":{"rendered":"How To Prevent Widget Titles Appearing On Your WordPress Site"},"content":{"rendered":"<p>The humble default WordPress Text widget is arguably one of the most flexible widgets but it has a flaw.<\/p>\n<p>Give it a title for clarity in the Admin interface and it automatically gets output in your WordPress site.<\/p>\n<p>In this quick article, I&#8217;ll show you how stop that automatic output and let you have your Admin clarity.<\/p>\n<p><!--more--><\/p>\n<figure id=\"attachment_123885\" class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-ratio-large wp-image-123885\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/12\/flexible-widget-titles-700x228.png\" alt=\"Composite image of admin interface and sidebar\" width=\"700\" height=\"228\" \/><figcaption class=\"wp-caption-text\">Giving your Text widgets titles adds real clarity<\/figcaption><\/figure>\n\n<p>The description of &#8220;add arbitrary text and HTML&#8221; shortchanges this widget&#8217;s capabilities as we all know that we can also include javascript and, with a bit of tweaking, shortcodes.<\/p>\n<figure id=\"attachment_123883\" class=\"wp-caption alignright\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-123883\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/12\/flwt-before.png\" alt=\"Screenshot of the Primary Sidebar with 3 text widget areas\" width=\"293\" height=\"308\" \/><figcaption class=\"wp-caption-text\">What&#8217;s in each Text widget?<\/figcaption><\/figure>\n<p>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.<\/p>\n<p>I wanted to give my widgets a title so I knew which was which but I didn&#8217;t necessarily want the titles being output in the public site (the default behavior).<\/p>\n<p>The solution, it turns out, is dead simple and a mere three lines of code.<\/p>\n<p>There are certain mantras that seem to always run true for WordPress one of which is &#8220;there&#8217;s a filter or action for that&#8221;. In this case, it&#8217;s a filter, more specifically the widget_title filter which is applied to every widget title before being output in a site&#8217;s sidebar or widget area.<\/p>\n<figure id=\"attachment_123882\" class=\"wp-caption alignright\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-123882\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/12\/flw_after.png\" alt=\"Screen shot of same sidebar with titles on Text widgets\" width=\"294\" height=\"304\" \/><figcaption class=\"wp-caption-text\">Clarity in Admin, hidden to public<\/figcaption><\/figure>\n<p>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 &#8211; [Admin interface only].<\/p>\n<p>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&#8217;ll return an empty string, otherwise the original title is returned and ultimately displayed in the public site.<\/p>\n<p>As this filter is not applied in the Admin interface, I still get my titles.<\/p>\n<p>Here&#8217;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.<\/p>\n<p><code><\/code><\/p>\n<p>&lt;?php<br \/>\n\/**<br \/>\n* @package Flexible_Widget_Titles<br \/>\n* @version 1.0<br \/>\n*\/<br \/>\n\/*<br \/>\nPlugin Name: Flexible Widget Titles<br \/>\nPlugin URI: https:\/\/wpmudev.com\/blog<br \/>\nDescription: Wrap a widget title in [] to prevent it being displayed on your website<br \/>\nAuthor: Chris Knowles<br \/>\nVersion: 1.0<br \/>\nAuthor URI: https:\/\/wpmudev.com\/blog<br \/>\n*\/<\/p>\n<p>function flexible_widget_titles( $widget_title ) {<\/p>\n<p>\/\/ get rid of any leading and trailing spaces<\/p>\n<p>$title = trim( $widget_title );<\/p>\n<p>\/\/ check the first and last character, if [ and ] set the title to empty<\/p>\n<p>if ( $title[0] == &#8216;[&#8216; &amp;&amp; $title[strlen($title) &#8211; 1] == &#8216;]&#8217; ) $title = &#8221;;<\/p>\n<p>return $title;<\/p>\n<p>}<\/p>\n<p>add_filter( &#8216;widget_title&#8217;, &#8216;flexible_widget_titles&#8217; );<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>This has certainly made managing multiple Text widgets a lot easier for me. Hopefully you might find it useful too.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>You want to give a WordPress Text widget a title for clarity in the Admin interface but you can&#8217;t because the default behaviour is for that title to be output on the public site. In this quick article, I&#8217;ll show you how stop that automatic output and let you have your Admin clarity.<\/p>\n","protected":false},"author":262394,"featured_media":123885,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[10211,52],"tutorials_categories":[],"class_list":["post-123878","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-admin","tag-widgets"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/123878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/262394"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=123878"}],"version-history":[{"count":2,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/123878\/revisions"}],"predecessor-version":[{"id":216098,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/123878\/revisions\/216098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/123885"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=123878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=123878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=123878"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=123878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}