Posted on Monday 24th of December 2007 at 10:04 in Tutorials

Add Wordpress filter removes content spacing

While playing with Wordpress plugin development for my how to write a Wordpress plugin series of articles I've recently stumbled across a slightly annoying problem when you add filters.

Filters in Wordpress are something that happens to content between the database and the page; this means that you can make modifications to the content without making changes to the information stored in the database. While this is clearly one of the aspects that slows Wordpress down the most (making them subject to death under high traffic).

When adding a filter, formatting is lost?
While adding a filter to my Wordpress content, the output was affected in ways that the filter shouldn't have managed: Above you'll see a version of my filter. All it should do is get the content and replace all instances of a word with a blank hyperlink of that word. Unfortunately content that once looked like this:

Rawr here is some content formatted.

Just a bit more content to prove a point. Rawr lah blah blah rawr all your base blah blah


Whereas after applying that filter, the same content was outputted like:

Rawr here is some content formatted.Just a bit more content to prove a point. Rawr lah blah blah rawr all your base blah blah


All spacing was removed while the rest of the formatting remained the same. No HTML was lost nor was any content, but all the line breaks seemed to be removed. Therefore the fix I applied was: Adding the nl2br function to the content restored the content spacing. I'm not sure why this occured because my str_replace shouldn't have made an impact on anything that wasn't 'but', but clearly it did. Anyway using the nl2br function you can restore the formatting - just thought I'd document this.

Check back soon for episode 2 in my how to write a Wordpress plugin series where I'll explain more about writing filters.

 

Enjoy this article? Why not subscribe to the full RSS feed?

Add Your Comments








Comments

Showing most recent 1 of 1 comments

THANK YOU! That was a big help.

Subscribe to the RSS Feed

Stay up to date with Seopher.com by subscribing to the RSS feed, either in your browser or subscribe via email using the form below

Updates by Email

By subscribing by email you’re also subscribing to the Seopher.com newsletter; a periodical email outlining new reviews, competitions and other subscriber-only content

  • 125x125 banner only $50 pcm
  • Dreamhost Hosting $5.95 per month
Want to give your product/website exposure?

Paying for a featured review is a great way to give your product, service or website exposure. For as little as $50 you can have a full review on the site forever.

Advertising Bundle! Review + Banner = $70

To kick start the new improved Seopher.com, buy a review and get a 125x125 advert half price. Your banner gets displayed on over 450 pages for a full month.