{"id":543,"date":"2012-03-30T14:00:27","date_gmt":"2012-03-30T18:00:27","guid":{"rendered":"http:\/\/www.jetmore.org\/john\/blog\/?p=543"},"modified":"2013-01-21T14:53:43","modified_gmt":"2013-01-21T18:53:43","slug":"multiple-spaces-after-period-in-wordpress","status":"publish","type":"post","link":"https:\/\/www.jetmore.org\/john\/blog\/2012\/03\/multiple-spaces-after-period-in-wordpress\/","title":{"rendered":"Multiple Spaces After Period in WordPress"},"content":{"rendered":"<p>It&#8217;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.<\/p>\n<div id=\"attachment_545\" style=\"width: 254px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.jetmore.org\/john\/blog\/wp-content\/uploads\/2012\/03\/Capture.png\"><img aria-describedby=\"caption-attachment-545\" decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.jetmore.org\/john\/blog\/wp-content\/uploads\/2012\/03\/Capture.png\" alt=\"\" title=\"Capture\" width=\"244\" height=\"167\" class=\"size-full wp-image-545\" \/><\/a><p id=\"caption-attachment-545\" class=\"wp-caption-text\">Fear my drawing skills...<\/p><\/div>\n<p><!--more--><\/p>\n<p>I finally got grumpy enough to try to find a solution for it today.  The obvious &#8220;edit every old post to remove double-spaces&#8221; seemed inefficient, and frankly I&#8217;m not likely to retrain myself to use only one space for future posts.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Next, after looking at the page source and seeing that there were two spaces present and not, say &#8216;&amp;nbsp;&#8217; characters (*foreshadowing*), I tried to reproduce the problem outside of WP, mimicking the WP CSS.  No luck.<\/p>\n<p>I finally managed to Google up this <a href=\"http:\/\/wordpress.org\/support\/topic\/weird-spacing-between-sentences\">WP support page<\/a> where the reporter is describing the exact issue I am having.  What&#8217;s interesting to me is that this is a relatively new ticket and he&#8217;s told that there&#8217;s no solution.  It does point out what&#8217;s actually happening though:<\/p>\n<blockquote><p>What happens is that when you put two spaces in a row in the visual editor, wordpress converts it to &#8220;space + unicode_nbsp&#8221;<\/p><\/blockquote>\n<p>Ah.  That&#8217;s right, always use tools that won&#8217;t lie to me.  Of course view-source is just translating that unicode character (correctly) to a space.  That&#8217;s what the character is supposed to look like.  I can confirm that&#8217;s what&#8217;s happening though.  I grabbed a copy of a post with wget and looked at the output with xxd:<\/p>\n<div id=\"attachment_550\" style=\"width: 481px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.jetmore.org\/john\/blog\/wp-content\/uploads\/2012\/03\/Capture21.png\"><img aria-describedby=\"caption-attachment-550\" decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.jetmore.org\/john\/blog\/wp-content\/uploads\/2012\/03\/Capture21.png\" alt=\"\" title=\"Capture2\" width=\"471\" height=\"32\" class=\"size-full wp-image-550\" srcset=\"https:\/\/www.jetmore.org\/john\/blog\/wp-content\/uploads\/2012\/03\/Capture21.png 471w, https:\/\/www.jetmore.org\/john\/blog\/wp-content\/uploads\/2012\/03\/Capture21-300x20.png 300w\" sizes=\"(max-width: 471px) 100vw, 471px\" \/><\/a><p id=\"caption-attachment-550\" class=\"wp-caption-text\">&quot;20&quot; is the real space, &quot;c2a0&quot; is  U+00A0, the Unicode non-breaking space.<\/p><\/div>\n<p>While that page itself doesn&#8217;t offer a solution, it mentions <a href=\"http:\/\/core.trac.wordpress.org\/ticket\/19936\">WP bug 19936<\/a>, 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 &#8220;Visual&#8221; 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.<\/p>\n<p>Again, none of that really helps me right now, and I suspect that it won&#8217;t help old posts in the future either because, once it&#8217;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&#8217;t released yet, so that&#8217;s not a help on newer posts.<\/p>\n<p>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.<\/p>\n<p>I still don&#8217;t have a solution for old posts that I created in the visual editor.  I toyed with the idea of creating some javascript to do it dynamically but I don&#8217;t really care enough to.  I may go back and edit the articles that still show up on the main page, but in the end just knowing the cause of the problem and how to avoid\/fix it in the future is enough for right now.<\/p>\n<p><strong>Update:<\/strong> I actually went back and edited the last 10 posts, replacing the unicode_nonbreak with a regular ASCII space character.  I wasn&#8217;t going to, but I figured out a relatively easy way to do it.  I had feared I wouldn&#8217;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&#8217;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.<\/p>\n<p><strong>Update 2, 2013-01-21:<\/strong> John Girdwood <a href=\"#comment-97\">suggests<\/a> a filter in the comments.  I can&#8217;t vouch for it and won&#8217;t be testing it (because it&#8217;s not an active aggravation for me anymore) but if anyone else does and wants to comment on it I&#8217;d love to hear about it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;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 &hellip; <a href=\"https:\/\/www.jetmore.org\/john\/blog\/2012\/03\/multiple-spaces-after-period-in-wordpress\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[8,14],"_links":{"self":[{"href":"https:\/\/www.jetmore.org\/john\/blog\/wp-json\/wp\/v2\/posts\/543"}],"collection":[{"href":"https:\/\/www.jetmore.org\/john\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jetmore.org\/john\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jetmore.org\/john\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jetmore.org\/john\/blog\/wp-json\/wp\/v2\/comments?post=543"}],"version-history":[{"count":10,"href":"https:\/\/www.jetmore.org\/john\/blog\/wp-json\/wp\/v2\/posts\/543\/revisions"}],"predecessor-version":[{"id":569,"href":"https:\/\/www.jetmore.org\/john\/blog\/wp-json\/wp\/v2\/posts\/543\/revisions\/569"}],"wp:attachment":[{"href":"https:\/\/www.jetmore.org\/john\/blog\/wp-json\/wp\/v2\/media?parent=543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jetmore.org\/john\/blog\/wp-json\/wp\/v2\/categories?post=543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jetmore.org\/john\/blog\/wp-json\/wp\/v2\/tags?post=543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}