WordPress.org

Ready to get started?Download WordPress

Forums

Replace gallery_shortcode function (8 posts)

  1. BC
    Member
    Posted 6 years ago #

    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?

  2. BC
    Member
    Posted 6 years ago #

    * bump *

  3. Aesqe
    Member
    Posted 6 years ago #

    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');
  4. BC
    Member
    Posted 6 years ago #

    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.

  5. Aesqe
    Member
    Posted 6 years ago #

    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);
  6. BC
    Member
    Posted 6 years ago #

    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.

  7. Aesqe
    Member
    Posted 6 years ago #

    No problem :)

  8. Bad Feather
    Member
    Posted 5 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic