Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm experimenting with doing "microblogging" with WordPress and I'm doubtful about how to handle "titles" when one uses different post "formats" (the "formats" may or may not be related with the core WP functionality). Many microblogging services don't feature any kind of title field, yet when a title is needed for technical reasons, like HTML page title, the permalink or the RSS post title, it is auto-generated from meta data (like the post date) or from a short excerpt of the text in the post. There are cases when the user puts no text at all like with images or videos (which is not a good practice, but that's another issue); in those cases, in Tumblr at least, the HTML page title is absent, the permalink is only the post 'id' and the RSS title is merely the name of the "format" of the post.

I'm in a dilemma about which could be the best practice for handling empty titles, in cases where is optional to have them:

  1. Auto-generating the title from the first characters in the content of the post. The theme has to have a function to not show the title when it coincides with the first characters of the "content". That's how the P2 theme works, yet besides of the auto-generating title function itself, I don't know if the option of conditionally not showing the title in the loop can be used in a generic plugin. Also, there may be cases when is not clear if the title is auto-generated (and can be auto-replaced) or actually a custom input.
  2. Don't auto-generate the post title at all and leaving them empty, but still generate an automatic title for HTML page titles, post slugs and RSS feeds. This may solve some issues with the previous method, yet I don't know how to generate HTML page titles and RSS feed titles without creating whole custom templates instead of a simple function. Although I feel having posts in the dashboard with no title may be awkward to manage.
share|improve this question

3 Answers 3

The post title cannot really be empty, because it's used to create the slug of the post, which is often used in the permalink.

Use the p2 approach and autogenerate the title. This solves a lot of problems. Whether you show that title in the theme or not is up to you.

share|improve this answer
    
Thanks, although the title field can be empty (specially in custom post types since 3.3), and when that occurs, the post slug is generated from the post ID. Post 'slugs' can also be manually input, yet sadly is not a common practice. Although post slugs being the same as post ID causes some problems on WP due to a bug. It's easy to auto-generate it from elsewere with a function similar to the ones mentioned for the title. –  peroyomas Nov 19 '11 at 20:38
1  
Numbers as slugs are crap. They look bad, they behave poorly, etc. Give it some text. –  Otto Nov 19 '11 at 20:41

I have written a simple plugin to create post titles on 'save_post': Fix Empty Titles.
When and where a title is displayed to the visitor is up to the theme author. You could check the current post’s post format.

share|improve this answer

I found another way. More or less based on a Konstantin Kovshenin method. Not sure if mark this as correct or not. It may case performance issues, but still. First, make a filter to make the default "title" template tags fill empty title fields by the first letters of the content.

add_filter( 'the_title', 'empty_title_fix' );
add_filter( 'single_post_title', 'empty_title_fix');
function empty_title_fix( $titles ) {
if ( strlen( trim( $titles ) ) < 1 ) {
    $daa = get_post($idd);
    $conten = $daa->post_content;
    $contents = trim(strip_tags($conten));
    if (strlen($contents) > 40) {
        $titles = substr($contents, 0, 40) . '...';
//this is if the content has no words (like with a single image or video). It can be different.
    } elseif (strlen($contents) == 0) {
        $format = get_post_format();
        if ( false === $format ) {
            $format = 'post';

        }
        $titles = ucfirst($format);
        } else {
            $titles = substr($contents, 0, 40);
        }
    }
return $titles;
}

This is for autogenerating post slugs with a similar method to the above, in order to replace the 'numeric' ones.

add_action('publish_post', 'insert_automatic_post_slug', 10, 2);
function insert_automatic_post_slug($postID, $post) {
        if($post->post_name == $postID) {
            $content = trim(strip_tags($post->post_content));
            if (strlen($content) > 40) {
                $title = substr($content, 0, 40) . '...';
            } elseif (strlen($content) == 0) {
                $format = get_post_format();
                if ( false === $format ) {
                    $format = 'post';
                }
                $title = ucfirst($format);
            } else {
                $title = substr($content, 0, 40);
            }
            $post->post_name = sanitize_title($title);
            $result = wp_update_post($post);
            }
}

Then all the title tags anywere are replaced by the first characters of the content or by some other fallback. One manually in the theme may choose if the title tags displays if empty.

//Simplified version to get the point across.
<?php $my_id = get_the_ID();
$post_id = get_post($my_id);
$post_title = $post_id->post_title;
if (!empty($post_title)) { ?>
<h1 class="entry-title"><?php the_title(); ?></h1>  
<?php } ?>
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.