WordPress One Liner to Remove Style Tags from Posts using [gallery] Shortcode

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.

About Matt

IF YOU REALLY want to hear about it, the first thing you'll probably want to know is where I was born, and what my lousy childhood was like, and how my parents were occupied and all before they had me, and all that David Copperfield kind of crap, but I don't feel like going into it, if you want to know the truth.

In any case I am employed as a Senior Linux Systems Engineer at Rackspace in San Antonio, TX.

I spent the majority of my college years as an Astrophysics major specializing in black holes. When I got to my senior year and was studying 40 hours a week to stay on track I decided to bail and pursue an easier career.

I spend the majority of my time now contributing to WordPress, developing WordPress plugins, helping out in the WordPress IRC Channel, playing football (not American!), practicing Krav Maga, and last but not least, spending time with my Wife and Son.

This entry was posted in Code, One Liner, PHP, Snippet, WordPress. Bookmark the permalink.

11 Responses to WordPress One Liner to Remove Style Tags from Posts using [gallery] Shortcode

  1. Thanks man, very useful!

  2. 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.

  3. Dan says:

    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.

  4. Pingback: Wordpress Gallery Styles entfernen | knevels·org

  5. Ross says:

    Thanks for this. Do you know how to remove the br clear:both as well?

  6. alex says:

    Hi there,

    Tx for the tip/code.

    Do you know how to add and replace gallery-item for a custom class

  7. Exactly what I was looking for. Much thanks.

  8. Been trying to do this myself with partial success but couldn’t understand where all the styling had gone. great post, thanks

  9. Arnd says:

    Man, you’re a lifesaver! Exactly what I was looking for, felt just the same.

    Thx a lot indeed! :-) Arnd

  10. more easy

    add_filter('use_default_gallery_style', '__return_false');

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre>