Remove double space after a period

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.

Read More

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.

Related posts

Leave a Reply

2 comments

  1. 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.