3

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

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.

4 Answers 4

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);
}
8
  • thank you, but doesn't work! If I apply that function to the_content the entire content is gone. If I apply it to post_gallery nothing happens. Commented Feb 26, 2011 at 11:01
  • Try preg_replace('/'.preg_quote('<br\s+style="\s*clear\s*:\s*both[;]{0,1}\s*"[/]{0,1}>', '/').'/Ui', '', $output);.
    – wyrfel
    Commented Feb 26, 2011 at 11:39
  • 1
    ok...to simplify, i usually just do this: '/\<br[^\>]*\>/'
    – wyrfel
    Commented Feb 26, 2011 at 12:24
  • thank you it works. but only with the updated version of @keatch and the regexp of @wyrfel. Thank you both for your help. Awesome. Thank you Commented Feb 26, 2011 at 12:50
  • Yay...teamwork! ;-)
    – wyrfel
    Commented Feb 26, 2011 at 13:03
3

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; }

1
  • thank you, that works fine. I'm already doing that - and I'm even using jquery to remove() the br element. I just thought there might be a way so the br's doesn't even get set. Commented Feb 26, 2011 at 12:19
0

You can use Justin Tadlock's Cleaner Gallery plugin to get clean markup.

0

Preg replace should contains apostrophes.

return preg_replace('/<br style="(.*)">/mi','',$output);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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