WordPress.org

Ready to get started?Download WordPress

Forums

[gallery] (6 posts)

  1. elfin
    Moderator
    Posted 6 years ago #

    On trying out the gallery I notice that for some reason the style associated with this is being added into the content. Surely this should be added to the header with add_action ('wp_head',...

    Line 373 of media.php refers.

    Should this be changed? Or am I out on my on with this one?

    [edit: it has already been reported]

  2. elfin
    Moderator
    Posted 6 years ago #

    Just noticed line 412 is missing an end slash as well.
    [edit: it has already been reported]

    It is also a shame that this feature doesn't appear to work with previously uploaded images either :(

  3. Dickie
    Member
    Posted 6 years ago #

    It's worse than that as well... because the CSS is added to the content, it is added for every post that uses it.
    This means that if you have different layout/css for different gallery pages (e.g. 3/4 column layout [gallery columns="3"] etc.) Then when viewing an archive, or an overall blog view when more than one post is on a page at a time, then the last shown post will override the gallery style... Not good.

  4. Evita
    Member
    Posted 6 years ago #

    Copy the css to your stylesheet and delete it in media.php

  5. Dickie
    Member
    Posted 6 years ago #

    Or better still, rather than hack the core code...
    write a plugin to do it...

    Something like this should do the trick, copy all the code and paste into a file called gallerycss.php, make sure you have no white space before or after the php tahs (<? and ?>), upload to your plugins folder and activate. (At your own risk)

    <?php
    /*
    Plugin Name: Move Gallery CSS from body to head
    Plugin URI: http://wordpress.org/support/topic/164825?replies=5#post-722329
    Description: Removes the Gallery CSS declaration and adds into the head
    Author: Dickie
    Version: 1.0
    Author URI:
    */
    
    function test_gallery_removecss($css)
    {
    	// Note you still need to return the start div
    	return '<div class=\'gallery\'>';
    }
    
    function test_gallery_addcss()
    {
    	// Note this is limited to 3 wide column width only
    	echo '<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>';
    }
    add_filter('gallery_style', 'test_gallery_removecss');
    add_action('wp_head','test_gallery_addcss');
    ?>

    Note though that using this you would no longer be able to use the column reference in the gallery shortcode, as that is calculated into the CSS code we have just overridden.
    (e.g. [gallery columns="4"] ), to do that you need to change the %age width in the css in the plugin.

  6. SpencerLavery
    Member
    Posted 6 years ago #

    Yeah I noted this just before the official release on the test list, a patch was created but wasn't implemented in time. I'm sure it will be fixed in the next release.

Topic Closed

This topic has been closed to new replies.

About this Topic