Jeremy Herve Avatar

That’s me. And my blog. In English and in French.


WordPress: how to add a default title to your posts

This came up on the Fediverse: @[email protected] wanted to automatically add titles to the posts on his microblog.

This automation would have to be compatible with all the editors, including the mobile app.

I think may be a fairly common request when one uses their WordPress site as microblogging platform. When you share quick updates with your friends, be it a picture, a video, a sentence or two, you don’t really want to have to think of a title for that update. The editor should enable you to share, not get in the way.

I gave this a try with the following code snippet:

add_action(
	'save_post_post',
	'jeherve_add_default_title',
	10,
	3
);

/**
 * Automatically add today's date and time to all posts
 * that do not already have a title.
 * This is triggered when a post is saved, regardless of where you are saving.
 *
 * @param int     $post_id Post ID.
 * @param WP_Post Post object.
 * @param bool    $updated Whether this is an existing post being updated.
 */
function jeherve_add_default_title( $post_id, $post, $updated ) {
	// If we already have a post title, bail.
	if ( ! empty( $post->post_title ) ) {
		return;
	}

	// Build our title.
	$today         = new DateTimeImmutable(
		'now',
		wp_timezone()
	);
	$default_title = $today->format('j F Y H:i');

	// unhook this function so it doesn't get triggered when we will save a hew version of the post with a new title.
    remove_action( 'save_post_post', 'jeherve_add_default_title', 10, 3 );

        // Update post.
        wp_update_post(
			array(
				'ID' => $post_id,
				'post_title' => $default_title,
			)
		);

        // Re-hook function.
        add_action( 'save_post_post', 'jeherve_add_default_title', 10, 3 );
}

It appears to work well for me, including in the mobile app:

Funny enough, this was a full mobile experience for me: I found the toot, created a site, added a functionality plugin, wrote the code snippet, tested it in my browser, added the site to the mobile app, tested the snippet in there, switched to this site, wrote this post, switched back to the browser to add a code block to the post with the snippet, and published. All from my daughter’s bed 🙂

I wouldn’t call the experience smooth, it took a few detours, but the Internet is still a pretty magic experience in the palm of our hands, when you think about it 🙂


Edit, April 8, 2023: in the first version of this post, I failed to account for timezones. You can even see the problem on my video! I’ve now updated my snippet accordingly.

Reactions on the Fediverse and on the ATmosphere

Continuez votre lecture / Keep reading

Discover more posts about , or look at some of the posts suggested below.

Jeremy Herve
Jeremy Herve

WordPress, TV Series, music, kids, and board games. I think that’s probably the best way to define me in a few words. 🙂

I work at Automattic where I lead a team building tools for bloggers and creators. I talk a lot about WordPress things, but also about all things open source in general.

I post in English and in French.

I live in Brittany, France, so you’ll also find me sharing pictures from our beautiful region from time to time.

1,241 posts
195 followers