One of the biggest things that irritates me when using the [gallery] shortcode is that style tags are inserted into the post content. Why does this irritate me? Because this does not validate as XHTML 1.0. Luckily the fix is quite easy. Add the following code as a plugin or to your themes functions.php and say goodbye to the style tags in your post content.
add_filter('gallery_style', create_function('$a', 'return preg_replace("%<style type=\'text/css\'>(.*?)</style>%s", "", $a);'));
One side effect to doing this is that the CSS normally outputted to into your post content is no longer output at all. You will need to add the CSS to your themes css file, usually style.css. The following css is the default output:
<style type='text/css'>
.gallery {
margin: auto;
}
.gallery-item {
float: left;
margin-top: 10px;
text-align: center;
width: 33%;
}
.gallery img {
border: 2px solid #cfcfcf;
}
.gallery-caption {
margin-left: 0;
}
</style>
The width for .gallery-item is a dynamic value determined by the columns attribute specified in the [gallery] shortcode. The default is 3 columns which makes the width 33%, floor(100/$columns).
I have also written a plugin called Gallery Shortcode Style to Head which does a forward lookup to determine if a post uses the [gallery] shortcode and if it does, prints the default CSS to the head. While the plugin works and performs its function, I still prefer a solution that has less overhead associated with it, hence the code provided in this post.
Thanks man, very useful!
Wow! Thanks so much! I was having the hardest time getting my gallery’s columns to fit within the content area because I couldn’t control the CSS. And now I can! Woohoo! Thanks again.
Great great.. it helps a whole lot… do you know how to remove the links from the gallery images? I don’t want any link at all.
Pingback: Wordpress Gallery Styles entfernen | knevels·org
Thanks for this. Do you know how to remove the br clear:both as well?
Hi there,
Tx for the tip/code.
Do you know how to add and replace gallery-item for a custom class
Exactly what I was looking for. Much thanks.
Been trying to do this myself with partial success but couldn’t understand where all the styling had gone. great post, thanks
Man, you’re a lifesaver! Exactly what I was looking for, felt just the same.
Thx a lot indeed! :-) Arnd
more easy
add_filter('use_default_gallery_style', '__return_false');That is correct, and indeed easier. However, please take note that it was not added until WordPress 3.1.