{"id":45905,"date":"2022-08-09T22:16:15","date_gmt":"2022-08-09T22:16:15","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=45905"},"modified":"2026-10-10T20:58:31","modified_gmt":"2026-10-10T20:58:31","slug":"remove-widget-title-conditionally","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/remove-widget-title-conditionally\/","title":{"rendered":"Hide a WordPress Widget Title With an Exclamation Mark"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Classic WordPress widgets print their title above the content. Sometimes you want the title for yourself, so you can tell five Custom HTML widgets apart in the admin, but you do not want it on the site.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With this snippet you hide a WordPress widget title by starting it with an exclamation mark. <code>!Newsletter form<\/code> shows nothing on the front end and keeps its name in <strong>Appearance \u2192 Widgets<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Hide a classic widget's title when it starts with \"!\".\n *\/\nadd_filter( 'widget_title', 'dpc_hide_widget_title' );\n\nfunction dpc_hide_widget_title( $title ) {\n\t$title = (string) $title;\n\n\tif ( '!' === substr( ltrim( $title ), 0, 1 ) ) {\n\t\treturn '';\n\t}\n\n\treturn $title;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Classic widgets pass their title through the <code>widget_title<\/code> filter before printing it, and they print the heading only when the title is not empty.<\/li>\n\n\n<li>The callback returns an empty string for a title that starts with <code>!<\/code> (leading spaces are ignored), so the widget skips the heading and its <code>before_title<\/code>\/<code>after_title<\/code> markup. Every other title is returned unchanged.<\/li>\n\n\n<li>The old version returned nothing (<code>null<\/code>) for hidden titles. Themes and plugins that pass the filtered title to string functions then hit PHP 8.1 &#8220;passing null&#8221; deprecation notices. This version always returns a string, and casts a <code>null<\/code> title coming in as well.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor and publish. Leave <strong>Action\/Hook<\/strong> empty. Widgets render on the front end, in the Customizer preview and in the Legacy Widget block preview, and Everywhere covers all of them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same code also works unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A different marker: replace <code>'!'<\/code> with your own character, for example <code>'#'<\/code>. For a longer prefix such as <code>&#91;hide&#93;<\/code>, compare with <code>str_starts_with( ltrim( $title ), '&#91;hide&#93;' )<\/code>.<\/li>\n\n\n<li>Hide every title in one sidebar instead: register the sidebar with empty <code>before_title<\/code> and <code>after_title<\/code>, or hide <code>.widget-title<\/code> in that sidebar with CSS.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This works for classic widgets and for classic widgets inside the Legacy Widget block. Block widgets (Heading, Paragraph and so on) have no title field: delete the Heading block instead.<\/li>\n\n\n<li>If your site uses the block-based widget screen and you want the classic one back, see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/restore-classic-widgets-wordpress\/\">Restore Classic Widgets in WordPress Without a Plugin<\/a>.<\/li>\n\n\n<li>To change the heading level of the titles you keep, see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/widget-title-change-h2-to-h4\/\">Widget Title change h2 to h4<\/a>.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Hide the title of any classic WordPress widget by starting it with an exclamation mark, and keep the title in the widget admin.<\/p>\n","protected":false},"author":5,"featured_media":46870,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Hide a WordPress Widget Title With an Exclamation Mark","description":"Hide the title of any classic WordPress widget by starting it with an exclamation mark, and keep the title in the widget admin."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[55,10],"dpc_library":[204],"class_list":["post-45905","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-must-have","tag-tips"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=45905"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45905\/revisions"}],"predecessor-version":[{"id":46869,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45905\/revisions\/46869"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46870"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=45905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=45905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=45905"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=45905"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}