How do I convert a page's title to lower case?

All we need is an easy explanation of the problem, so here it is.

On my test case, the page’ title is “Privacy”. The straightforward approach

strtolower(the_title())

also returns “Privacy” — still with a capital P. I also tried

mb_strtolower(the_title())

and

mb_strtolower(the_title(), 'utf8')

with similar results.

Not sure if it matters, but my wp-config.php has

define('WPLANG', '');
define('DB_CHARSET', 'utf8');

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

lowercasing the title

If I’m understanding you correctly, you should be doing:

strtolower(get_the_title());

or

print strtolower(get_the_title());

if you want to display it. Below is an explanation as to why.

the_title() vs. get_the_title()

The function the_title() prints the current post’s title unless you pass false as its third argument. Unless you call it like:

$title = the_title('', '', false);

The title will be printed, and the $title variable won’t contain anything. This matters because calling strtolower() on an empty variable doesn’t do very much.

You want to use get_the_title() function in most cases where you’re looking to fill a variable with the content posts title.

Note, however, that if you’re not currently in a loop, you’ll need to pass a post ID to get_the_title(). In almost all cases when on a single post or page you can do this by using:

get_the_title($post->ID);

as the $post variable should be in the global scope.

Method 2

I think the best method for this is by using CSS (text-transform: lowercase).

But if you want to use PHP, WP for this you can use: <h1><?php echo strtolower( get_the_title() ); ?></h1>

Method 3

Here is a great plugin for doing this. It’s on my website, WordPress.org, Github and others. It’s 100% free, with no membership versions. Please feel free to download it. This does it on the data level, which allows you to change it later, or do it on some and not others. It’s a good option, and as I said. Works Great!

My Site:

https://properprogramming.com/tools/wp-change-titles-case/

WordPress:
https://wordpress.org/plugins/change-titles-case/

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply