***NOTICE***

All plugin information has moved to http://plugins.baptiste.us
Comments are now closed. Please use the new forums instead

Xinha for WordPress was my first plugin - so it was definitely a learning experience.  The WordPress Codex was very helpful, but there still was a lot of trial and error involved in understanding how various actions and other parts of the API worked. But I also learned that no matter how hard you try, you can’t anticipate all impacts of your code or what type of setup other people will have. Needless to say I ran into a few unexpected things with v1.0 of Xinha4WP. One of the interesting ones was Text Control, Xinha, and a chicken and egg problem.

WordPress by default uses a filter called wpautop to format posts.  If you have a double line feed in a post, it will insert a paragraph tag (<p>) automatically as the post is rendered for the user. Single line feeds will optionally insert <br /> tags as well for an HTML line break.  This way authors don’t have to worry about inserting HTML tags for paragraphs and line breaks while they are writing up a post.  The posts are stored in the database raw (i.e. just the linefeeds and line breaks) - wpautop is run and the tags are inserted when the post is displayed.

Xinha is more advanced as an editor and handles all the HTML generation on the fly.  When you hit return in Xinha, a paragraph <p> tag is inserted automatically and a new paragraph is started. Shift-return will cause a simple line break with a <br /> tag automatically.  So where normal WordPress posts insert the line spacing tags when a post is displayed - Xinha inserts them on the fly when the post is written (and thus the <br /> and <p> tags are saved to the database record for the post)  Thus is you author a post in Xinha and then view it and it goes through wpautop, you’ll get double tags and other wild results.

Because of this, when I wrote Xinha4WP, I inserted a warning in the Post Options screen recommending you turn off dynamic post formatting (wpautop) using the Text Control plugin.  Easy enough.  Xinha takes care of all the backend HTML formatting for you, and Text Control ensures the Xinha generated HTML is left alone when each post is rendered and viewed. So what’s the problem?

Well, my blogs are not very big and I’ve been using Xinha with WordPress via a hand configured plugin/hack for all my posts since I switched to WordPress on my blogs.  That means almost all of my blog posts were Xinha formatted when written.  But what if you a have a couple hundred posts in WordPress already when you install Xinha4WP and turn off Post text formatting with Text Control?  Your new posts will look fine.  But all of your old posts that looked fine when wpautop was active will now be one long continuous paragraph - no line breaks at all.

Whoops.

So that presents a bit of a chicken and egg problem.  You need to disable wpautop for any new posts created with Xinha, but if you do it via the default option screen, you’ll mess up all the formatting of your old posts.  So what can you do? Don’t worry - you don’t have to go back and re-edit your old posts! Smiley

The good news is there is a way to get around this problem.  If you have a sizable number of blog posts already, which were authored in the normal Post/Quicktags screen yet want to start using Xinha, leave Post Formatting in Text Control set to wpautop.  This will ensure all your old posts are filtered through wpautop and are formatted properly.  One of the nice things about Text Control is you can set the Post Formatting value for each post. Now for any NEW posts you write with Xinha, set Post Formatting to No Formatting. Wait, you say, you don’t see the Text Control option when writing a new post?  No worries.  Text Control v2.0b1 only displays the per post Text Control menus when editing a post.  I’ve written a patch for Text Control that fixes this so you can set the Text Control Post Formatting when you first write a post in Xinha.  Simply download this new version of text-control.php and overwrite it in your plugins directory. The Text Control options will now appear below the Xinha editor section for both new posts and edited posts.

So if you have an established blog and want to start using Xinha, I recommend you leave the default Text Control for Posts set to wpautop (and ignore the warning in Xinha4WP - I’ll tweak it some in v1.2).  Then for any NEW posts you write with Xinha, make sure Text Control for Posts is set to No Formatting. And don’t worry - WordPress saves your post properly regardless of this setting.  If you forget to flip it to No Formatting during an initial draft, you can always flip it later while editing the post in question without worry.

If you have a new blog and plan to use Xinha with it from the start or have a very small number of existing posts, I recommend setting the default Post Formatting in Text Control to No Formatting. But now those of you with large existing blogs can also use Xinha and maintain your old post formats.

Hopefully this clears up some of the confusion surrounding post pre-formatting (with Xinha) and post-formattng (with wpautop) and how to get them to play nice.