0

I have a ton of posts that we created, but unfortunately there are a lot of blank spaces. This is an example of what I see on the backend, in the actual post:

<h1>This is a post title</h1>

&nbsp;



&nbsp;

Words here. More words.

&nbsp;

&nbsp;




Words down here.

So, when that is rendered on the page on the Front end, there are extra line breaks that I'd like to get rid of.

Is there a way to loop through the actual posts on the backend? Then I can (somehow) check for 2+ empty lines and/or &nbsp;, and remove them.

Or - a solution to the symptom, not the problem - would I be better off somehow using CSS to remove such lines?

1

Since there are no tags around the space &nbsp; characters, they will be compressed on the page output.

So the visual output will not be affected. And a very minor effect on the database or processing of the post content.

If you are really concerned about the extra stuff, then you could use the_content filter to remove extra &nbsp; in the content. But you would have to be careful that your filter didn't remove &nbsp; that really needed to be in the content.

Which is why I don't think that the effort to remove is worth it, and may cause unexpected problems.

Added

If you really want to get rid of the extra spaces in the posts, then see the answer here, where the wp_insert_post_data filter is used. That filter fires when the post is saved via the editor.

You'll just need to adjust the regex for the text you are looking to replace. Look at a example post in text editing mode to get the actual string to look for in the regex. (Regex is hard for the amatuer - and maybe the experts - but you might look at this Regex testing site to tweak your regex so it works: https://regex101.com/ . I like that site, because it explains things and also creates the PHP code needed to do the Regex.)

| improve this answer | |
  • Good points, I didn't consider that! So, since I believe the Post page (where you actually create the post) is pretty much WYSIWYG, is there a way I could easily remove those extra lines? (Sorry, I edited my OP example to include additional 'blank' newlines) – BruceWayne Oct 12 '18 at 20:21
  • See what I added to the answer. – Rick Hellewell Oct 12 '18 at 22:18
  • This is definitely the way to go, thanks for your answer and comments! I will now try implementing. – BruceWayne Oct 15 '18 at 20:11
0

Navigate in your theme files and look for where the content is printing.

You will be looking for a line of code similar to this:

the_content();

Replace it with this:

$content = get_the_content(); //grabs the content
$content = apply_filters('the_content', $content); //adds edit filter
$content = str_replace('<p>&nbsp;</p>', '', $content); //replaces line break
echo $content; //prints content

It will remove all instances of <p>&nbsp;</p> with an empty string. This only changes it on the front end, not actually changing how your posts are saved in the database.

| improve this answer | |
  • 1
    Although that might work, it is not best, IMHO: 1) this only works on displayed content; 2) you don't want to change core or theme code (unless you do a child theme); 3) it is better to use an add_filter('the_content', 'your-function') to 'hook' into the display; 4) and doesn't affect new posts; 5) and only affects the front end (as you mention); although I suspect you won't see those blank links. – Rick Hellewell Oct 12 '18 at 22:20
  • @RickHellewell I totally agree – Liam Stewart Oct 12 '18 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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