Advertisements
Connect with us

Tips & Tricks

Remove spaces when echoing the_title

Ever wanted to print or echo the WordPress title attribute without spaces?

Advertisements

This little trick can be useful for calling custom functions and printing the title with out spaces for W3C compatibility. I used this trick in a new theme called Galleria, which will be out for public download in the coming days.

Using this comes in handy for a delicious text link:

<?php $title = get_the_title(); ?>
<a href="http://del.icio.us/post?url=<?php echo $title; ?>&amp;<?php echo str_replace(" ", "%20", $title); ?>">
Bookmark This (<?php echo $title; ?>)</a>

What I am doing is calling $title = get_the_title(); and using str_replace(" ", "%20", $title); to replace empty spaces with a %20, which is used in URL encoding empty spaces.

Alternatively you can use a dash or underscore.

Thanks to Jason Boyle for his adaption.

Advertisements
4 Comments

4 Comments

  1. Peter

    May 16, 2009 at 12:10 am

    If you’re just trying to make the string into a valid URL candidate, urlencode() might be a better choice- that way you don’t have to worry about other characters in your title (colons, commas, punctuation, etc).

    Peter´s last blog post..How to Hide Certain Custom Fields From the Edit Post Page

  2. Peter Kahoun

    May 16, 2009 at 7:58 am

    There is as “urlencode()” php function for this purpose. http://php.net/manual/en/function.urlencode.php

  3. Austin

    May 16, 2009 at 8:31 am

    Thanks for that!

  4. Matt

    August 16, 2009 at 11:58 pm

    Brilliant stuff, thanks!

You must be logged in to post a comment Login

Random Search Terms

Title

Top Articles

powered by WassUp

Recent Posts: Fully Net Worth . com

Ryan Garcia Net Worth: How Much is the Boxer Worth Actually?

Ryan Garcia Net Worth: How Much is the Boxer Worth Actually?

Ryan Garcia is an American professional boxer who competes in the lightweight division. Most recently, he became the winner of the WBO–NABO and WBC Silver lightweight title. He recently signed a deal with Golden Boy Promotions, which is said to be one of the most lucrative boxing deals for a prospect. As of 2019, Ryan […]

Quin NFN Net Worth: How Much Money Does the Rapper Make?

Quin NFN Net Worth: How Much Money Does the Rapper Make?

Quin NFN is an emerging American rapper who has released songs such as “Talkin’ My Shit” and “Gametime Pt. 2”. He was born on January 18, 2001, in Austin, Texas, United States. As of 2019, Quin NFN net worth is estimated to be $200,000. Though Austin has been the home of some big stars in […]

Kembe X Net Worth: How Much Money Does Kembe X Make?

Kembe X Net Worth: How Much Money Does Kembe X Make?

Kembe X is an American rapper who is best known for his songs such as “Dollar Please”, “Extremely Good”, and “Schemin On The Scoochies”. As of 2019, Kembe X net worth is estimated to be $200,000. Kembe X initially found some success through his debut mixtape called Self Rule but had a dip in the […]

%d bloggers like this: