I have a following code and it is so annoying that post_name is automatically converted to lowercase.
$post = array(
'post_name' => 'SampleWord',
),
);
// assume $post have other necessary fields as well
$post_id = wp_insert_post( $post, true );
When I check the post_name after above code it returns 'sampleword'
.
I’ve checked the documentation of wp_insert_post
and it mentioned 'post_name'
field would be sanitized.
But converting to lowercase characters is not about sanitization.
And how to prevent it?
2
Answers
That’s an easy one to confuse because of the names but post_name is really the slug and the documentation warns you it’ll sanitize it (as you noted) but sanitize does also lowercase things.
Try the post_title addition above and see if it works out for you.
By default the post name is formatted and converted to lower-cased by the filter sanitize_title which hooks into sanitize_title_with_dashes(). As a solution you can replace the filter and omit the lower-case component.