I've been trying to add a filter in Wordpress to suppress empty paragraphs. I found a number of alternatives online (here is one of them https://gomakethings.com/remove-empty-paragraphs-added-by-wordpress/ ) but, so far, have failed miserably. After testing various hypothesis, I've found this strange regex behaviour happening:
I've created and tested this small script by itself (all it does is replace a space with a dash):
<?php
$content = 'abc def';
$patterns = array('#\s#');
$replacements = array('-');
$new_content = preg_replace($patterns, $replacements, $content);
echo $new_content;
The result, predictably was:
abc-def
Than I've added the same code as a filter in wordpress:
add_filter('the_content', array($this,'empty_paragraph_fix'),20,1);
function empty_paragraph($content) {
$patterns = array('#\s*#');
$replacements = array('-');
$content = preg_replace($patterns, $replacements, $content);
return $content;
}
But this time it gave me some very strange results. On a content that originally had something like this:
<div class="campus-list">
<ul class= ...
It returned this:
-<-d-i-v--c-l-a-s-s-=-"-c-a-m-p-u-s---l-i-s-t--"-->--<-u-l--c-l-a-s-s-=-
What am I missing here? Have you ever seen anything like this?