For A Shadier Tomorrow

Use Default Title for Posts Without Title

This article deals with an edge case, where a post doesn’t have any title. Sure your WordPress site will most likely worked just fine even with all those titleless posts. But you know what? It makes thing looked darn lame! Just look at this pic.

When a post has no title

Did you see how lame it is?

Anyway, today we’ll learn about this simple PHP snippet to make sure that no empty title will be shown on the front-end.

The Code

add_filter( 'the_title', 'shadythemesDefaultTitle', 10, 2 );
function shadythemesDefaultTitle( $title, $id = null ) {
	$default = 'Untitled';
	if ( $title == '' ) { return $default; }
	return $title;

See the highlighted 3rd line? That is our default title. Feel free to change it into whatever you want.

You can simply copy-paste the code in your theme’s functions.php or use it as a plugin, both will work just fine.

With the code running in the system, the precious example will turn into the following:

No more empty title!

Oh and btw… the code didn’t change anything in the database. It just checks post_titles that’s going to be printed to the front-end for any post_titles with empty value. And if it’s indeed empty, replace it with our default title. That’s it!

That’s all for now!

Write a comment

Your email address will not be published. Required fields are marked *

About ShadyThemes

ShadyThemes is a shady group for hire.

We build shady websites using shady tools for whatever shady purpose the shady clients has in their shady mind.

As you may have guessed, everything you see around ShadyThemes website is coded by us, so I guess you can see them as our portfolios.

Just send us a message if you need our service.

Contact Form

Neo Armstrong Cyclone Jet Armstrong Cannon