Support » Fixing WordPress » Replace gallery_shortcode function

  • BC

    (@brokencrust)


    The gallery_shortcode function in media.php has an embedded css stylesheet in it. This means that once you use a gallery your page will no longer be XHTML compliant (due to a stylesheet being added to the document outside of the header). This is easily sorted by removing the offending css code from the function and putting it in style.css and using a inline style statement for the dynamic width part.

    However I’d like to be able to distribute this fix inside a theme without having to punt a hacked media.php. Is there any way to replace the gallery_shortcode function using functions.php?

    In short, is the gallery_shortcode function pluggable in a theme?

Viewing 7 replies - 1 through 7 (of 7 total)
  • BC

    (@brokencrust)

    * bump *

    try placing this in your theme’s functions.php file:

    function remove_gallery_css()
    {
    	return "<div class='gallery'>";
    }
    add_filter('gallery_style', 'remove_gallery_css');
    BC

    (@brokencrust)

    Thanks for the hint – I did think of doing this (and it should fix the css error), but a filter is not enough because the function also has html hard coded into it (two clear: both statements) which don’t work with my theme as the sidebar is a floated div and it means that the second row of pictures will be placed under it. I need to replace the whole function but I’m pretty sure that can’t be done.

    I customised media.php on my site, but I can’t expect other users on my theme to do this. I guess the built in gallery function is still new and maybe in the next release it will be more compatible.

    then you’ll also need to add this code in addition to the first one 🙂
    it took me a while to realize that the function wasn’t doing anything because its priority was too low, hence the number 11 at the end.

    function fix_gallery_output( $output )
    {
    	$output = preg_replace("%<br style=.*clear: both.* />%", "", $output);
    
    	return $output;
    }
    add_filter('the_content', 'fix_gallery_output',11, 1);
    BC

    (@brokencrust)

    I’ve learned a bit more about filters now. This solution was pretty close but there was no way to pass in the $itemwidth variable. So in the end I just replaced the shortcode function with my own version in function.php and all was well. (I should have done this first 🙁 )

    Thanks for all your help.

    No problem 🙂

    Is it possible to create an additional filter that removes the links from the images? Or is there a hidden option to do this within the shortcode?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Replace gallery_shortcode function’ is closed to new replies.