Reputation: 17
I am trying to verify if the WordPress post title starts with certain 3 first characters to achieve a condition. Here is what i tried by it brings back the title itself.
$verify = the_title();
if(substr($verify,0,3) == '000'){
$maptitle = 'test 1';
} else {
$maptitle = 'none';}
Upvotes: 0
Views: 84
Reputation: 20286
Use "exact" comparison because otherwise it will be compared by casting types to 0 or false and it will show you misleading records
change the if to
if (substr(get_the_title(), 0, 3) === '000') {
pay attention there are three =
Upvotes: 0
Reputation: 12332
How to apply your filter at edit time using https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
function my_wp_insert_post_data_filter( $data )
{
if( substr( $data['post_title'], 0, 3 ) == '000' )
{
$data['post_title'] = 'test 1';
}
else
{
$data['post_title'] = 'none';
}
return $data;
}
add_filter( 'wp_insert_post_data', 'my_wp_insert_post_data_filter' );
Upvotes: 0
Reputation: 4810
You should use get_the_title()
. the_title()
will print the title to the screen. In your case, you need to return the title so that you can parse it.
$verify = get_the_title();
https://developer.wordpress.org/reference/functions/get_the_title/
If you want to use the_title()
, you can, but you will need to specify that you want the value returned.
$verify = the_title('', '', false);
https://developer.wordpress.org/reference/functions/get_the_title/
Upvotes: 1