<?php
/*
Plugin Name: Title auto-generation
Plugin URI: http://justinsomnia.org/
Description: Auto-generate titles on post publication based on initial content of post
Version: 0
Author: Justin Watt
Author URI: http://justinsomnia.org/
*/

function auto_generate_title($post)
{
	$title_length = 50;
	$ellipsis     = "...";

	if (trim($post['post_title']) != '') {
		return $post;
	}

	$title = '';
	$content = preg_replace('|\s+|', ' ', strip_tags($post['content']));
	
	foreach ( explode(' ', $content) as $word ) {
		if ( (strlen($title) + strlen($word)) > $title_length ) {
			if ( empty($title) )
				$title = substr($content, 0, $title_length);

			if ( !empty($title) )
				$title = trim($title) . $ellipsis;

			break;
		}
		$title .= $word . " ";
	}
	
	$post['post_title'] = trim($title);

	return $post;
}

add_filter('post_publish_pre', 'auto_generate_title');

?>
