Hide a WordPress Widget Title With an Exclamation Mark

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.

With this snippet you hide a WordPress widget title by starting it with an exclamation mark. !Newsletter form shows nothing on the front end and keeps its name in Appearance → Widgets.

The snippet

<?php
/**
 * Hide a classic widget's title when it starts with "!".
 */
add_filter( 'widget_title', 'dpc_hide_widget_title' );

function dpc_hide_widget_title( $title ) {
	$title = (string) $title;

	if ( '!' === substr( ltrim( $title ), 0, 1 ) ) {
		return '';
	}

	return $title;
}

How it works

  • Classic widgets pass their title through the widget_title filter before printing it, and they print the heading only when the title is not empty.
  • The callback returns an empty string for a title that starts with ! (leading spaces are ignored), so the widget skips the heading and its before_title/after_title markup. Every other title is returned unchanged.
  • The old version returned nothing (null) for hidden titles. Themes and plugins that pass the filtered title to string functions then hit PHP 8.1 “passing null” deprecation notices. This version always returns a string, and casts a null title coming in as well.

Add it with Scripts Organizer

In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor and publish. Leave Action/Hook empty. Widgets render on the front end, in the Customizer preview and in the Legacy Widget block preview, and Everywhere covers all of them.

The same code also works unchanged in a small plugin or an mu-plugin.

Customise it

  • A different marker: replace '!' with your own character, for example '#'. For a longer prefix such as [hide], compare with str_starts_with( ltrim( $title ), '[hide]' ).
  • Hide every title in one sidebar instead: register the sidebar with empty before_title and after_title, or hide .widget-title in that sidebar with CSS.

Gotchas

  • 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.
  • If your site uses the block-based widget screen and you want the classic one back, see Restore Classic Widgets in WordPress Without a Plugin.
  • To change the heading level of the titles you keep, see Widget Title change h2 to h4.

In the Scripts Organizer library Import it with one click from Scripts Organizer.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You’ll also get DPlugins product updates. No spam, and you can unsubscribe anytime.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You'll also get DPlugins product updates. No spam, and you can unsubscribe anytime.