Hey guys,
I simply want to prevent the creation of empty paragraphs in my wordpress post. That happens quite often when trying to manually space content.
I don’t know why this doesn’t take effect?
/*Remove empty paragraph tags from the_content*/
function removeEmptyParagraphs($content) {
/*$pattern = "/<p[^>]*><\/p[^>]*>/";
$content = preg_replace($pattern, '', $content);*/
$content = str_replace("<p></p>","",$content);
return $content;
}
add_filter('the_content', 'removeEmptyParagraphs');
edit/update:
seems like the problem is this:
function qanda($content) {
// filters for [q=some question] and [a=some answer]
// wraps it inside of <div class="qanda"><div class="question"> </div><div class="answer"> </div></div>
$content = preg_replace('/[q=(.+?)].+?[a=(.+?)]/is', '<div class="qanda"><div class="question">$1</div><div class="answer">$2</div></div>', $content);
return $content;
}
add_filter('the_content', 'qanda');
i did this function myself to filter for a kind of shortcode pattern in my posts and pages. Even though in my backend the post is completely done without paragraphs and unnecessary spacings the outcome looks like this:
<div class="entry">
<p></p>
<div class="qanda">...</div>
<p></p>
<p></p>
<div class="qanda">...</div>
<p></p>
<p></p>
<div class="qanda">...</div>
</div>
any idea where this empty p’s come from?
WordPress will automatically insert
<p>
and</p>
tags which separate content breaks within a post or page. If, for some reason, you want or need to remove these, you can use either of the following code snippets.To completely disable the wpautop filter, you can use:
If you still want this to function try adding a later priority value to your filter something like:
I had the same problem you have. I just did a… let’s say… not very beautiful solution, but it works and so far it’s the only solution I have. I added a little JavaScript line. It needs jQuery, but I’m sure you can figure it out without.
This is my tiny JS:
This works for me!
Simply use CSS
I know this is already marked ‘solved’ but just for reference, here’s a function which does exactly what you want without having to add any markup to posts. Just put this in your theme’s functions.php:
This is from this gist: https://gist.github.com/1668216
You could just run your filter before that nasty
wpautop
hooks on and messes with the markup.That way, you’ve already converted what you need to by the time it hooks on, which does help in some cases.
Same approach than 2 answers before me,
but an updated regex, because his didn’t work for me.
the regex:
/<p>(?:s| )*?</p>/i
(non capture group looking for any number of either whitespace or
s inside p-tag, all case insenstive.I found this weird, but actually calling
the_content()
will insert paragraphs in the manner you describe. If you want the html code, basically like you entered it (the same as “view HTML” when editing), then useget_the_content()
which returns the content without formatting and paragraph tags.Since it returns it, make sure you use something like:
See also: http://codex.wordpress.org/Function_Reference/get_the_content
This will recursively remove all the empty html tags from the string
Pattern is taken from http://codesnap.blogspot.in/2011/04/recursively-remove-empty-html-tags.html
If you have
<p>
tags with whitespace in the content,go to your post or page an edit it not in visual style.
you would be find some
in there..Delete it and the empty
<p>
tags will disappear.In order to have only html content without
tags we can use the following loop to out put only the html without formatting of the post or page
If you are getting an empty
<p>
tag at the top of your page or post, it is usually caused by wrapping the<?php the_content();?>
in paragraph tags.PROBLEM
This is what causes the stray
<p>
tag at the top of the page/post:<p><?php the_content();?></p>
SOLUTION
If you remove the
<p>
tags and only have the template tag, you’ll solve the problem.<?php the_content();?>