Improve Gallery Short Code

  1. mantone


    The Gallery shortcode is a great step in the right direction.

    I think however it could be improved, instead of injecting the css code befere the gallery, it should only be done once, and preferably inserted in the head of the document. If you have multiple galleries on a page, it gets redundant.

    Second, instead having to always type in the gallery, maybe there could be a admin page that would keep track of which pages would show a gallery, a sort of check on and check off page.

    Those are just my thoughts. In the mean time i'm gong to try to find a way to turn off the css.

    Posted: 10 years ago #
  2. Dougal Campbell


    This really needs to be fixed. Putting the <style> tag in the body breaks xhtml validation. The only barrier is the fact that there's a variable size used (for number of columns) in the styles output (gallery-item width). This can be worked around, though it might be a bit ugly.

    We'd need to scan though $posts to see which ones contained the gallery shortcode. Then you'd have to target the gallery-item CSS selectors more tightly, based on the post-ID, generating a selector for each gallery in the current page view.

    And while we're at it, we could use some more filters in the gallery HTML generation...

    Posted: 9 years ago #
  3. mantone



    I found this nifty little plugin that addresses the issue: WordPress 2.7 places CSS for shortcode gallery inside the body html

    i have also used the following in my functions.php my template file, so I don't always have to insert [gallery] in every post i make that have uploaded photos. I'm lazy, what can I say.

    *** Quotes are messed up, remember to properly replace them in this Sample
    add_filter('the_content', 'insertGallery');
    function insertGallery($content) {
    	global $post;
    	return $content.do_shortcode('[gallery id=&quot;'.$post->ID.'&quot; size=&quot;thumbnail&quot; columns=&quot;3&quot;]');

    I have used, do_shortcode it in the image.php Template file. Basically it's the [gallery] shortcode with the right ID. Please note, that if you use it in a template file, the Gallery CSS won't spit out at all. So they won't be styled. I modified the plugin above to do so. Or you can just edit your own CSS sheet.

    *** Quotes are messed up, remember to properly replace them in this Sample
    echo do_shortcode('[gallery id=&quot;'.$post->post_parent.'&quot; size=&quot;thumbnail&quot; columns=&quot;4&quot;]');
    Posted: 9 years ago #
  4. Webstractions


    You can apply a filter to remove the inline style altogether.

    Add this to your functions.php:

    add_filter( 'gallery_style', 'my_gallery_style', 99 );
    function my_gallery_style() {
        return "<div class='gallery'>";

    This will effectively obliterate all of the inline styles that the Gallery Shortcode injects into the Html. Even gets rid of the reference to media.php in the comment following the inline style.

    The 99 argument in add_filter is to make sure that my_gallery_style is processed later in the queue.

    Posted: 9 years ago #
  5. Valerie

    Webstractions, thank you for that fix. It was exactly what I was looking for!

    Posted: 9 years ago #
  6. Thanks for the plugin, that may solve the issue.I'll try.

    Posted: 9 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.

  • Rating

    6 Votes
  • Status

    This is plugin territory