Solved

Need help removing whitespace from end of Wordpress post as it's saved

Posted on 2014-01-28
6
279 Views
Last Modified: 2014-02-16
I'm surprised this is not more of an issue on multi-author blogs. Certainly is with my writers -

They tend to leave extra <p></p> tags at the end of their posts, not realizing that white space (when viewed on the visual editor) is going to appear as empty space on their post when published.

To complicate it a bit, sometimes, depending on how they edited it, there will be styles in the tag, such as: <p style="text-align: justify;"></p>.

Is there a way to identify and trim all empty paragraph tags from the end of the post as it's being saved?

Thanks for any assistance,

Chris

edit - its occurred to me that I don't need to trim paragraphs only from the - end - of the post. I don't see any reason for any empty paragraph tags to exist in the content at all. So the solution could remove all empty p tags, not just the ones at the end. That would probably be easier.
0
Comment
Question by:St_Aug_Beach_Bum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 39815086
I'm surprised this is not more of an issue on multi-author blogs. Certainly is with my writers -

What?  You're not silently editing all other author's posts? :)

In all seriousness, this has come up in the community but not restricted to just the end of the content.

add_filter('the_content', 'remove_empty_p', 20, 1);
function remove_empty_p($content){
    $content = force_balance_tags($content);
    return preg_replace('#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content);
}

Open in new window


Source:  https://gist.github.com/1668216

That function should strip all empty p tags from the post.
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39815093
You could add some jquery code
$('p').each(function(){
  if ($(this).html()===''){
      $(this).remove();
  }
});

Open in new window

0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39815116
Scott:

$('p:empty').remove();

Open in new window


Equivalent? (Serious question....my JQuery-fu is weak)
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39815134
Looks better to me.    

I figured jquery for older posts but is there a way to easily go back and clean up the db?
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39815190
Well, my filter solution runs as the page is called so it works for all content over time.
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 39815218
What?  You're not silently editing all other author's posts? :)

Actually, I have been - trying to get away from that :)

I'm not sure how or where to put the jquery, so I'm leaning toward the filter.

Since I have been cleaning up posts in the past, those are not an issue.

Would there be a way to run this filter and take out those tags as the post is being saved? That way it would not have to run with every hit, correct?

(I don't use cacheing as I have alternating ad codes.)
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question