Remove Inline CSS and Line Breaks in WordPress Galleries

Inline CSS and line breaks <br style="clear" /> that are automatically added to the image galleries in WordPress is an example of how the needs of Automattic and WordPress.com influence the way new features are added to the WordPress core. So instead of editing every single theme on WordPress.com and adding the necessary CSS for galleries to look good in all of them, they decided to put it WordPress core.

Here is how to replace those double line breaks with a single <br /> and remove the inline CSS:

add_filter('the_content', 'remove_br_gallery', 11);
function remove_br_gallery($output) {
	return preg_replace('/(<br[^>]*>\s*){2,}/', '<br />', $output);
}

add_filter('use_default_gallery_style', '__return_false');

3 Comments

  1. Chip Bennett says:

    I think I’ll try to write a patch to “correct” in core these instances of inline CSS and other, similar hacks. I wonder if it would get accepted?

  2. kaspars says:

    That would be great, Chip! I still don’t know what is the best way to go about supporting themes that don’t have the CSS for galleries. The problem is that with the inline CSS, developers and users didn’t bother adding anything to their style.css because it looked good by default.

  3. Paul says:

    The problem is that with the inline CSS, developers and users didn’t bother adding anything to their style.css because it looked good by default.

    I think it’s the otherwise, this inline css injection is what bothering web developer the most, it gets in the way and very annoying.

    Currently, I use Cleaner Gallery ( Justin Tadlock of Themehybrid ) to fix these problem, and I’ve been hoping that core will remove this ugly inline thing soon.

Leave a Reply