WordPress: how to add a default title to your posts

A quick tip to automatically set a title for your blog 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:


 * 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 ) ) {
	// Build our title.
	$today         = new DateTimeImmutable(
	$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.
				'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.

Leave a Reply

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