It’s bugged me for a while that my WordPress posts would often have an extra space at the beginning of a line. It would only happen when it was a new sentence, and after a while I realized it was related to my habit of typing two spaces after a period.
I finally got grumpy enough to try to find a solution for it today. The obvious “edit every old post to remove double-spaces” seemed inefficient, and frankly I’m not likely to retrain myself to use only one space for future posts.
I assumed this would be a well known issue and there would a tuneable or a plugin to address it, but it turned out to be really hard to Google the right terms. I have actually decided not to change anything on my system based on my findings, but I wanted to document what I learned.
First, I did find a few plugins, but nothing that looked especially well-made. I never even installed one, because they all looked so suspect that I assumed they would reformat all my code snippets also. I care much more about the formatting in those code snippets than I do the double-space issue.
Next, after looking at the page source and seeing that there were two spaces present and not, say ‘ ’ characters (*foreshadowing*), I tried to reproduce the problem outside of WP, mimicking the WP CSS. No luck.
I finally managed to Google up this WP support page where the reporter is describing the exact issue I am having. What’s interesting to me is that this is a relatively new ticket and he’s told that there’s no solution. It does point out what’s actually happening though:
What happens is that when you put two spaces in a row in the visual editor, wordpress converts it to “space + unicode_nbsp”
Ah. That’s right, always use tools that won’t lie to me. Of course view-source is just translating that unicode character (correctly) to a space. That’s what the character is supposed to look like. I can confirm that’s what’s happening though. I grabbed a copy of a post with wget and looked at the output with xxd:
While that page itself doesn’t offer a solution, it mentions WP bug 19936, and that bug actually contains the solution. Essentially, the root of the problem is the TinyMCE editor that is used to provide the WYSIWYG functionality of the WP “Visual” editor. TinyMCE was the component translating trailing spaces into non-breaking spaces. That ticket is actually closed because the 3.4 release will include a newer version of TinyMCE which employs slightly better logic about which space to make non-breaking.
Again, none of that really helps me right now, and I suspect that it won’t help old posts in the future either because, once it’s been saved that way in the DB, the trailing space has already been translated to a non-breaking space (it just looks like a regular space in the text editor). Also, 3.4 isn’t released yet, so that’s not a help on newer posts.
What does work, though, is just using the HTML editor instead of the Visual editor. Normally I would say that this is a poor solution, but I had already sworn off the Visual editor in the past week after getting too fed up with how it irreparably mangles code snippets.
Update: I actually went back and edited the last 10 posts, replacing the unicode_nonbreak with a regular ASCII space character. I wasn’t going to, but I figured out a relatively easy way to do it. I had feared I wouldn’t be able to do a bulk find and replace for fear of harming other formatting. I found that a good text editor (I used Notepad++) can do a find and replace on the specific U+00A0 character very quickly. I couldn’t figure out how to type that character, but I could copy it from the post and paste it into the search box, then type a regular space into the replace box. Since the find/replace box stays open all the time, I could copy the text out of the post, paste it into n++, replace all, copy out of n++, and paste into WP very quickly. Once I figure out what I was doing it took about 3 minutes to fix those 10 posts. If I had a very large site I would probably write a tool to do a find/replace against the DB directly, but this solution worked fine for me.
Update 2, 2013-01-21: John Girdwood suggests a filter in the comments. I can’t vouch for it and won’t be testing it (because it’s not an active aggravation for me anymore) but if anyone else does and wants to comment on it I’d love to hear about it.