Ticket #3794: title-auto-generation.php

File title-auto-generation.php, 0.9 kB (added by Justinsomnia, 2 years ago)

proof-of-concept plugin for generating titles on post_publish_pre filter

Line 
1 <?php
2 /*
3 Plugin Name: Title auto-generation
4 Plugin URI: http://justinsomnia.org/
5 Description: Auto-generate titles on post publication based on initial content of post
6 Version: 0
7 Author: Justin Watt
8 Author URI: http://justinsomnia.org/
9 */
10
11 function auto_generate_title($post)
12 {
13     $title_length = 50;
14     $ellipsis     = "...";
15
16     if (trim($post['post_title']) != '') {
17         return $post;
18     }
19
20     $title = '';
21     $content = preg_replace('|\s+|', ' ', strip_tags($post['content']));
22     
23     foreach ( explode(' ', $content) as $word ) {
24         if ( (strlen($title) + strlen($word)) > $title_length ) {
25             if ( empty($title) )
26                 $title = substr($content, 0, $title_length);
27
28             if ( !empty($title) )
29                 $title = trim($title) . $ellipsis;
30
31             break;
32         }
33         $title .= $word . " ";
34     }
35     
36     $post['post_title'] = trim($title);
37
38     return $post;
39 }
40
41 add_filter('post_publish_pre', 'auto_generate_title');
42
43 ?>