Title: Replace gallery_shortcode function
Last modified: August 19, 2016

---

# Replace gallery_shortcode function

 *  Anonymous User 393930
 * (@anonymized-393930)
 * [17 years, 10 months ago](https://wordpress.org/support/topic/replace-gallery_shortcode-function/)
 * 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)

 *  Thread Starter Anonymous User 393930
 * (@anonymized-393930)
 * [17 years, 10 months ago](https://wordpress.org/support/topic/replace-gallery_shortcode-function/#post-822133)
 * * bump *
 *  [Aesqe](https://wordpress.org/support/users/aesqe/)
 * (@aesqe)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/replace-gallery_shortcode-function/#post-822174)
 * 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');
       ```
   
 *  Thread Starter Anonymous User 393930
 * (@anonymized-393930)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/replace-gallery_shortcode-function/#post-822187)
 * 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.
 *  [Aesqe](https://wordpress.org/support/users/aesqe/)
 * (@aesqe)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/replace-gallery_shortcode-function/#post-822189)
 * 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);
       ```
   
 *  Thread Starter Anonymous User 393930
 * (@anonymized-393930)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/replace-gallery_shortcode-function/#post-822193)
 * 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.
 *  [Aesqe](https://wordpress.org/support/users/aesqe/)
 * (@aesqe)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/replace-gallery_shortcode-function/#post-822198)
 * No problem 🙂
 *  [brad2dabone](https://wordpress.org/support/users/brad2dabone/)
 * (@brad2dabone)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/replace-gallery_shortcode-function/#post-822309)
 * 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.

## Tags

 * [gallery](https://wordpress.org/support/topic-tag/gallery/)
 * [gallery_shortcode](https://wordpress.org/support/topic-tag/gallery_shortcode/)
 * [pluggable](https://wordpress.org/support/topic-tag/pluggable/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 3 participants
 * Last reply from: [brad2dabone](https://wordpress.org/support/users/brad2dabone/)
 * Last activity: [17 years, 3 months ago](https://wordpress.org/support/topic/replace-gallery_shortcode-function/#post-822309)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
