<?php
/*
Plugin Name: Auto Generate Title
Author: Jatinder Pal Singh, Themummichogblog.com
Version: 1.2
Tags: post title, title, automatic, generator
Requires at least: 3.0.1
Tested up to: 5.4.2
Stable tag: 1.1
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Description: Auto Generate Title from the POST Content. If you leave Title blank, it will use the text written in [agt][/agt] within your post content. If you do not mention these shortcodes in your post, It will pick up first 30 characters from the post.
Changes:
1) $postdata2 = preg_replace(“/[^A-Za-z0-9 ]/”, ”, $postdata);
2) $data[‘post_title’] = substr($postdata2,0,30);
*/
add_shortcode(“agt”, “agt_shortcode”);
function agt_shortcode( $atts, $content = null ) {
return $content;
}
add_filter( ‘wp_insert_post_data’ , ‘auto_generate_title’ , ’99’, 2 );
function auto_generate_title( $data , $postarr )
{
$title_of_post = ltrim($data[‘post_title’]);
$length_of_title = strlen($title_of_post);
$postdata = ltrim($data[‘post_content’]);
//$postdata2 = preg_replace(“/[^A-Za-z0-9 ]/”, ”, $postdata);
$postdata2 = preg_replace (‘/[^\p{L}\p{N}]/u’, ‘ ‘, $postdata);
if($length_of_title<1)
{
$matchPattern = “/\[agt\](.*?)\[\/agt\]/”;
preg_match_all($matchPattern, $postdata, $foundIt, PREG_PATTERN_ORDER);
if (isset($foundIt[1][0]))
$data[‘post_title’] = $foundIt[1][0];
else
$data[‘post_title’] = substr($postdata2,0,30);
}
return $data;
}
?>
I am using it on my blog https://themummichoblog.com
However, whenever there was a special character in the beginning of the text I got a blank post due to special characters.
Modified the code to replace the post data and remove special characters. Using it with the current latest version of WordPress.
Ideally a new version is created so everyone can benefit.
—
<?php
/*
Plugin Name: Auto Generate Title
Author: Jatinder Pal Singh, Themummichogblog.com
Version: 1.2
Tags: post title, title, automatic, generator
Requires at least: 3.0.1
Tested up to: 5.4.2
Stable tag: 1.1
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Description: Auto Generate Title from the POST Content. If you leave Title blank, it will use the text written in [agt][/agt] within your post content. If you do not mention these shortcodes in your post, It will pick up first 30 characters from the post.
Changes:
1) $postdata2 = preg_replace(“/[^A-Za-z0-9 ]/”, ”, $postdata);
2) $data[‘post_title’] = substr($postdata2,0,30);
*/
add_shortcode(“agt”, “agt_shortcode”);
function agt_shortcode( $atts, $content = null ) {
return $content;
}
add_filter( ‘wp_insert_post_data’ , ‘auto_generate_title’ , ’99’, 2 );
function auto_generate_title( $data , $postarr )
{
$title_of_post = ltrim($data[‘post_title’]);
$length_of_title = strlen($title_of_post);
$postdata = ltrim($data[‘post_content’]);
$postdata2 = preg_replace(“/[^A-Za-z0-9 ]/”, ”, $postdata);
if($length_of_title<1)
{
$matchPattern = “/\[agt\](.*?)\[\/agt\]/”;
preg_match_all($matchPattern, $postdata, $foundIt, PREG_PATTERN_ORDER);
if (isset($foundIt[1][0]))
$data[‘post_title’] = $foundIt[1][0];
else
$data[‘post_title’] = substr($postdata2,0,30);
}
return $data;
}
?>
Hi
I tried this plugins and it’s amazing. because it can print the slugs also.
but i’m wondering to much more functional if we can add custom fields in title. for example : [author]-title-[date][postid] can be a title and the slugs also.
thanks. it’s great plugins.
]]>I’ve installed the plugin, but it’s not doing anything. It’s just there. I have empty titles, it’s not picking any of the first letters ofthe article.
]]>Can another option be added to change the title based on a phrase in the title already and then use the first 30 characters from content?
Most of my posts are posted by using the jetpack email to post function and the title is already entered.
For example, a post was entered with title “New Alert from:” then the first 30 characters used.
]]>