How to publish a post with empty title and empty content?

Written by  on November 22, 2016 

Why I’m doing this you might ask? I’m developing a website for a business with a portfolio where the gallery items are posts. These posts are populated through custom fields and category selections, nothing else.

For each item, there could be a title/content or not, it depends on the item in question and that’s entirely up to the client. They are the ones populating their portfolio and not me. I’m just building the system for them to easily publish whatever they want.

I realize that WordPress might have some issues with posts without a title, that’s why I implemented (in functions.php) a function to auto set a title for the post if none was attributed. This post auto title will never be shown on the website cause I look for it and omit it if it’s there, otherwise, show the attributed title to the post.

The function for that is:

add_action('publish_post', 'insert_automatic_post_title', 10, 2);

function insert_automatic_post_title($postID, $post) {
    if($post->post_title === '') {
        $post->post_title = '['.get_the_date('d/m/Y @ H:i:s').']';

        if($post->post_name == $postID) {
            $post->post_name = sanitize_title($post->post_title);
        }

        $result = wp_update_post($post);
    }
}

The only problem is that if title is empty and the content is also empty (with some custom fields already defined) that function above will never be called and the post won’t be published, it will remain in draft status.

The only solution I could think of was to create some shortcode like [empty_post] and ask the client to set that on the post content when he does not wish to attribute a title/content to the post. That shortcode could print something like <!-- EMPTY POST --> or even nothing at all. The fact that there’s something in the post content, the function above is called, an automatic title is set and the post is published.

But I’m looking for a way where the client doesn’t need to have more trouble (i.e: inserting a shortcode into the post content) than what’s needed, I want it to be as simple as possible. Without messing with WordPress source code of course.

Can anyone think of another way with actions/filters?

Category : Wordpress

Tags :