WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
You can publish posts with no content as long as you have a title, have you tried inserting the title using wp_insert_post_data – Wyck Sep 9 '11 at 2:20
    
Yes, I couldn't make it work... That function doesn't seem to be called when you press the "publish" button. At least it doesn't happen if you have an empty title/content. – Ricardo Amaral Sep 9 '11 at 12:25
up vote 8 down vote accepted
+50

If the post content, title and excerpt are empty WordPress will prevent the insertion of the post. You can trick WordPress by first filtering the input array so empty values are set to something else, and then later resetting these values back to empty strings. This will bypass the standard check.

add_filter('pre_post_title', 'wpse28021_mask_empty');
add_filter('pre_post_content', 'wpse28021_mask_empty');
function wpse28021_mask_empty($value)
{
    if ( empty($value) ) {
        return ' ';
    }
    return $value;
}

add_filter('wp_insert_post_data', 'wpse28021_unmask_empty');
function wpse28021_unmask_empty($data)
{
    if ( ' ' == $data['post_title'] ) {
        $data['post_title'] = '';
    }
    if ( ' ' == $data['post_content'] ) {
        $data['post_content'] = '';
    }
    return $data;
}
share|improve this answer
    
This solves it :) I'm now allowed to award the bounty for the time being, but I'll do it tomorrow. – Ricardo Amaral Sep 11 '11 at 22:04
1  
please jan... add a note that this actually breaks some behaviour. I spent almost 2 hours figuring this out. This breaks when you try to use wp_update_post(array("ID" => 233, "post_name" => "somecoolname")); it just does NOT work. To fix it you have to remove the filter, then call wp_insert_post again with the ID as argument. then it works again... please add that note man – Toskan Jan 14 at 8:15
1  
actually wp_insert_post with ID argument instead of update (does the same more or less) fixes the bug... but I think the bug is because you didn't add add_filter('wp_update_post_data', 'wpse28021_unmask_empty'); – Toskan Jan 14 at 8:25
    
@Toskan: It's been almost 5 years since I did some serious WordPress development, so I don't have an environment to test this quickly, but your comment makes sense, you probably need to unmask it on updates as well. I won't add it to the post (because I can't test it), but I think you can suggest an edit to the post itself and someone else could approve it? – Jan Fabry Jan 14 at 21:21
    
i actually think the post_name is generally a problem, I just happened to thought it is your filter. Keep the post the way it is. The fix for post_name not updating (when one is not admin) is actually using wp_insert_post instead of wp_update_post – Toskan Jan 17 at 0:13

A quick and dirty solution would be to use javascript/jQuery ... do some detection to check which post type it will be executing on, then you do the following:

  1. find the post title field

  2. hide the post title field

  3. auto populate the post title field

Most of the wordpress backend operates with javascript anyway, and the likelyhood of a user using the admin without JS is low. And even if that happens, you can detect and add a warning/message of some sort.

share|improve this answer
    
The Javascript is not a problem but that's not the kind of solution I'm looking for. I appreciate your suggestion nonetheless. – Ricardo Amaral Sep 11 '11 at 15:25

This plugin (Auto Title) is out of date but works well for me on my 4.3.1 install of WP. This could help you populate the title with custom fields and category selections as per your brief

share|improve this answer

I'd just wrap these in DIVs and use display:none in my CSS.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.