Add Wordpress filter removes content spacing
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?





Showing most recent 1 of 1 comments