Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I run a multiple author platform with more than 1000 writers. Most users writer their content externally (Microsoft Word, OpenOffice etc) and paste it into the Visual Editor. Around 20% of the articles have a double space after every single period.

Feedback from the users tells me that some versions of these programs adds two spaces after full stop instead of one. I initially linked these users to tutorials of how to address this in the external application itself however the growth of the platform is making this a ridiculously exhausting task.

Is there a way to remove all double spaces and convert them into single space on the post page? Maybe upon clicking publish. It would be ideal if it happened on the backend post page rather than the front end (through some styling fix) - although I am open to ideas.

On a personal note, I find it quite astonishing that this feature is not a part of the core functions of Wordpress given that at least 20% of my authors are experiencing it.

share|improve this question
1  
What exactly is the problem? Multiple spaces are rendered as just one space in HTML. Nobody should ever see two spaces. –  toscho Dec 6 '12 at 20:07
    
How come it is rendering two spaces on my platform? Can you kindly try to add two spaces in the visual editor and see how it displays? –  Amanda Duke Dec 6 '12 at 20:19
2  
@toscho Actually nevermind, I just ran a test on a fresh Wordpress install and it clearly adds two spaces, please check this example post: bogsorken.com/wpse/wordpress/?p=4 –  Amanda Duke Dec 6 '12 at 20:22
    
Ah, I see. TinyMCE turns the second space into a non-breaking space. This should be solved in JavaScript. –  toscho Dec 6 '12 at 20:50
    
@toscho Would you know how to solve this in Javascript? –  Amanda Duke Dec 6 '12 at 21:02
add comment

2 Answers

I cannot offer a JavaScript solution, because I am not sure where exactly this is happening.

But … we can hook into wp_insert_post_data and solve this issue in PHP:

add_filter( 'wp_insert_post_data', 't5_strip_double_spaces', 20 );

function t5_strip_double_spaces( $data )
{
    $data['post_content'] = preg_replace(
        "~( \x{C2}\x{A0}|\x{C2}\x{A0} )~m",
        ' ',
        $data['post_content']
    );
    return $data;
}

As plugin on GitHub.

share|improve this answer
    
I'll dig more later when I have time, but there might be a tiny_mce_init filter configuration that could work here, too. –  Chip Bennett Dec 6 '12 at 21:19
    
No, this will catch only occurrences of space+no-break-space, not regular double space. –  toscho Dec 6 '12 at 21:27
    
Excellent - will I need to update each published post for this to have effect? –  Amanda Duke Dec 6 '12 at 21:28
    
@ChipBennett If you have the time to find a tiny_mce_init filter, I would be grateful. –  Amanda Duke Dec 6 '12 at 21:30
    
You could send this function over all existing posts too. On the other hand, this should be possible in plain SQL. –  toscho Dec 6 '12 at 21:35
show 2 more comments

Add this to your functions.php file

<?php

function my_the_content_filter ($content)
{
    str_replace('.  ', '. ', $content);

    return $content;
}

add_filter('the_content, 'my_the_content_filter');

That should do the trick. Hope it helps!

Codex Page for Reference

share|improve this answer
1  
Interesting approach. Would this not interfere with any form of code that uses double space within it? I cannot think of any form of code in the content that would use double space on top of my head but the possibility exists, right? For instance, an image URL that has contains a double space... –  Amanda Duke Dec 6 '12 at 21:01
    
URLs shouldn't have a double space in the first place, but I did think about that possibility. In my edit, I added '. ' to target ONLY the double spaces that come after a full stop. –  Alex Lane Dec 6 '12 at 21:12
    
I just tested this code and it gave me a Parse error: syntax error, unexpected T_STRING error. Do you know what the issue is? Also, can you make it so that it detects double spaces after : and ! and ? –  Amanda Duke Dec 8 '12 at 16:53
    
Awwww. I was typing fast and made a silly little typo. Change the line add_filter('the_content, 'my_the_content_filter'); to add_filter('the_content', 'my_the_content_filter'); I have problems with quotes sometimes. –  Alex Lane Dec 9 '12 at 4:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.