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.

My writer has a tendency to add extra newlines (Enter key) and spaces (space bar) at the ends of posts. Sometimes this results in inches of extra whitespace onscreen!

Is there a way I can make sure extra trailing whitespace is removed when he hits "Update" in the Wordpress post editor?

share|improve this question
    
You could threaten him... –  markcbain May 25 at 7:17

4 Answers 4

Quick guess would be:

add_filter('content_save_pre', 'trim');

Add to functions.php of theme or wherever is appropriate in your site.

share|improve this answer
    
Doesn't work. Maybe it's because the post is still full of trailing  s ? –  a paid nerd Aug 30 '11 at 4:16
    
@a paid nerd likely that. trim() only removes true whitespace. It worked for me in quick test, but I used HTML editor. Likely your writer is using visual one that encodes spaces. You will need to hook your own function that will delete (or convert to regular spaces before trim) those entities. –  Rarst Aug 30 '11 at 8:49

This will stop the trailing non-breaking spaces from displaying without actually modifying the post in the database when added to functions.php (or wherever your theme stores user added functions).

function trim_post_trailing_whitespace($content){   
// use preg_replace to convert   into an unused character "☺" (ALT 1)
// then use rtrim to remove trailing unused character(s) "☺"
// now use rtrim again to remove trailing whitespace 
// now convert unused character(s) "☺" back into   in case used elsewhere
// Return


    $content = preg_replace("/ /", "☺", $content);
    $content = rtrim($content, "☺");
    $content = rtrim($content);
    $content = preg_replace("/☺/", " ", $content);
    return $content;

/* All the above simplified into a single equation  
    return preg_replace("/☺/", " ", rtrim(rtrim(preg_replace("/ /", "☺", $content), "☺")));   
*/
}

add_filter('the_content', 'trim_post_trailing_whitespace', 0);
// The '0' priority means do this first.
share|improve this answer

The 'problem' here is that wordpress converts 'enters' into paragraphs (< p >). Trailing and leading paragraphs result in large white spaces messing up your design. The wp (wysiwyg) editor is setup pretty spaciously and this often leads a user into believing the content is clean.

Check this very simple plugin, Space Remover, it removes these unintended paragraphs and gives you control over this functionality as well. (in case you want to use that extra space on some posts/pages)

share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

1  
Answers should be more than plain links. They should actually be an answer instead of a route where someone will maybe find an answer. Please help preventing link rot, edit your answer and provide the needed information that helps the OP as well as later visitors to solve their problem. –  kaiser Nov 14 at 12:29
    
@john23klipp Thank you! –  a paid nerd Nov 14 at 19:01

If you want to remove not only one but several trailing non-breaking spaces, you could use a variation of UncaJoes code which also uses the default php rtrim charlist.

function trim_post_trailing_whitespace( $content ) {
    $content = preg_replace( '/&nbsp;/', '☺', $content );
    $content = rtrim( $content, '☺' . " \t\n\r\0\x0B" );
    $content = preg_replace( '/☺/', '&nbsp;', $content );
    return $content;
}
add_filter( 'the_content', 'trim_post_trailing_whitespace', 0 );
share|improve this answer

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.