Jan 10, 2009
Avoid Post Defacement Due To wpautop / wptexturize
As you might notice there's an ad at the bottom of the post. And it's added by a plugin using the the_content filter. My plugin has an option to produce JS output using document.write so the robots don't index it and leak the link juice but I bumped into a problem. The wpautop internal wordpress filter was messing up the JS code, breaking lines and taking names leaving my heroic attempt at a cool plugin almost dead on the battlefield. But perseverence is a defining trait of mine ... and I'm lazy. So I started looking into the matter and everyone suggested disabling the filter.
Disabling The Default Filters Is Not An Option
... unless you are willing to actually work more. If you do disable the filter you can no longer use the HTML editor as it's meant to be. The double enters don't convert to paragraphs anymore and that is messed up. You need to HTML hand craft your posts for them to look as they should good. But I'm lazy so I had to find a workaround to fix this. I didn't want to write my own plugin to convert the paragraphs and fix the SCRIPT breaking.
I looked at the wpautop function and noticed it's supposed to skip script/style or something but it's not working right for me. I didn't want to hack the core of Wordpress! I want updates to work smoothly and not give me headaches.
So I Figured Out A Solution!
Each filter is assigned a priority when you add it using add_filter. And it has a default value of 10. This allows you to change the order of processing and add filters to run before or after the predefined filters or decide an order between your own plugins' filters. And I noticed the default the_content Wordpress filters are all added with default priority of 10.
I gave it a try and added a priority of 11 to my JS adding filter and it worked. My filter occured after the wpautop / wptexturize and the likes and the JS code is no longer broken.
Hope this helps someone. I banged my head against the wall to fix this for a lot more than I should have had.


Good tip. I am working on two Wordpress plugins myself, and although I have not encountered a problem like yours, its good to know that banging my head against the wall is not the only option, and research a bit more ;-)
Wordpress plugin development is not actually that hard, but if you want your plugin to do something a bit more than the developers planned for, you are sure to bang your head at least once in a while ;-)
Cheers.