Skip to content

WordPress get_post_gallery() Gutenberg Polyfill

I’m working on a new WordPress theme designed for Gutenberg, however Gutenberg isn’t finished, and there are elements that won’t work until the plugin is merged with core.

One such issue, that I ran into this weekend, is that get_post_gallery() doesn’t work with Gutenberg gallery blocks. There’s an open Github issue (and trac ticket) about this, but that’s no help since I don’t want to wait for the final release. So I made a small polyfill that replaces the code, and have added it to Github so anyone can use it.

The bit that does the hard work is only 4 lines long, it’s a regex that checks for the gallery code and then returns the required html.

The code itself is actually quite short. A lot of the polyfill is making sure that Gutenberg is enabled, and in use on the current blog post.

I am currently using this locally, for development, and as an mu-plugin on my theme demo site so that I can test the theme with production ready content. Once 5.0 drops I can delete the script and move on. Simples.

Thanks to Gabor Javorszky for help with the regex.

Ben View All

Ben is a lifelong Nintendo fan who also likes to build websites, and develop games. He also buys way too much Lego.

Leave a Reply

Your email address will not be published. Required fields are marked *