add_filter to post-gallery and remove all <br>’s?

hey guys,
i really need your help.

Whenever I use a gallery in wordpress and set it’s columns to just 1 WordPress automatically adds <br style="clear: both"/> after every <dl class="gallery-item">. I really need to prevent this behaviour and therefore I’d like to add a filter to my functions.php

Read More

The following to examples don’t work.

add_filter( 'post_gallery', 'remove_br_gallery', 9);

function remove_br_gallery($output) {

    return preg_replace('#<br*.?>#is', '',  $output);

}  


add_filter( 'the_content', 'remove_br_gallery', 9);

function remove_br_gallery($output) {

    return preg_replace('#<br*.?>#is', '',  $output);

}

Neither does this:

return str_replace('<br style="clear: both">', '',  $output);  

Any idea how I could solve that? I just don’t want to have any <br style="clear: both"/> inside of my galleries.

Related posts

Leave a Reply

4 comments

  1. EDIT:
    you must call your filter after the shortcode is processed, giving it priority > 10, and you must match on a multiline expression.

    Try this work with my installation and using the standard gallery shortag:

    add_filter( 'the_content', 'remove_br_gallery', 11, 2);
    function remove_br_gallery($output) {
        return preg_replace('/<br style=(.*)>/mi','',$output);
    }
    
  2. Or, alternatively, you could try disabling the effect of the <br>‘s via a stylesheet override, for instance:

    body dl.gallery-item + br { display: none; }