views:

1473

answers:

6

There seems to be a bug in a Wordpress PHP function that leaves whitespace in front of the title of the page generated by <?php echo wp_title(''); ?> I've been through the Wordpress docs and forums on that function without any luck.

I'm using it this way <body id="<?php echo wp_title(''); ?>"> in order to generate an HTML body tag with the id of the page title.

So what I need to do is strip that white space, so that the body tag looks like this <body id="mypage"> instead of this <body id=" mypage">

The extra white space kills the CSS I'm trying to use to highlight menu items of the active page. When I manually add a correct body tag without the white space, my CSS works.

So how would I strip the white space? Thanks, Mark


Part Two of the Epic

John, A hex dump was a good idea; it shows the white space as two "20" spaces. But all solutions that strip leading spaces and white space didn't.

And, <?php ob_start(); $title = wp_title(''); ob_end_clean(); echo $title; ?>

gives me < body id ="">

and <?php ob_start(); $title = wp_title(''); echo $title; ?>

gives me < body id =" mypage">

Puzzle. The root of the problem is that wp_title has optional page title leading characters - that look like chevrons - that are supposed to be dropped when the option is false, and they are, but white space gets dumped in.

Is there a nuclear option?


Yup, tried them both before; they still return two leading spaces... arrgg

+9  A: 

ltrim()

Rob
+13  A: 
John Kugelman
You definately get the prize for scope and variety, and they all should work. But something is wacky with the Wordpress function and I still have whitespace. Firebug shows it as whitespace, not any sort of character....
songdogtech
Part Two of the Epic is tacked onto my original question!
songdogtech
Tried them both before; they still return two leading spaces.... arrg.
songdogtech
What's up with the picture?
MiseryIndex
I'm Batman! http://www.imdb.com/title/tt0096895/quotes
John Kugelman
Why not use PHP to convert the resulting string to HEX and see what character code is being used at the beginning? Or you could always just take a substr() and clip off the first character.
Chris Thompson
That picture made me laugh waaay more than it should have. I was just expecting a technical response and ran into it and it caught me totally off-guard.
Paolo Bergantino
lool. someone deserves a medal.
bucabay
`<body id="<?php trim(wp_title('', false)); ?>">` returns `<body id="">`And `<?php echo substr(wp_title(''), 1); ?>` won't trim the leading spcaes, either...Very strange. But John deserves 1000 points for all this...
songdogtech
I might need 2000 if I have to go install Wordpress to answer this question!
John Kugelman
Wait, you're mixing those up. You need to pass false AND do an echo. Try `<?php echo trim(wp_title('', false)); ?>`.
John Kugelman
Jeez. How did I screw that up? Not fully understanding the docs and not watching what I was doing here. And up too late and half asleep this morning, too. This works fine:`<body id="<?php echo trim(wp_title('', false)); ?>">`Really appreciate it. You deserve a all expenses paid vacation at the Bat Cave, with unlimited miles on the Batmobile....
songdogtech
The leading spaces in wp_title the Wordpress function appear to be a bug that isn't going be fixed as it will break many themes, according to their bug tracking system. I posted John's resolution in the Wordpress forums...
songdogtech
+1  A: 
ltrim($str)
Mark
+1  A: 

Just to throw in some variety here: trim

 <body id="<?=trim(wp_title('', false));?>">
Chris Thompson
A: 

Thanks for this info! I was in the same boat in that I needed to generate page ids for CSS purposes based on the page title and the above solution worked beautifully.

I ended up having an additional hurdle in that some pages have titles with embedded spaces, so I ended up coding this:

<?php echo str_replace(' ','-',trim(wp_title('',false))); ?>
Tomdkat