1

I’m developing a custom WordPress theme and it’s my first time working with WordPress in a little while. I can see the post and page editor has been completely overhauled, and posts and pages can be built using “blocks”.

Previously, when creating custom themes I’d create a post.php template file and display the post content using <?php the_content(); ?>. This still works, but I’d like to change how a gallery block is rendered (I’d like to render images in a Bootstrap carousel rather than an image grid).

I looked at the template hierarchy page of the WordPress developer documentation, but couldn’t see anything relating to blocks on there.

So how do I go about updating the mark-up for the various blocks WordPress supports? If someone could point me to the relevant documentation, that would be a huge help!

1 Answer 1

1

If you want to adjust the HTML of a Core block, one option is to add a filter on the render_block hook. This allows you to use PHP to modify the HTML output, so you don't have to set up a whole Webpack setup and use newer JS. If you place this in the (child) theme, this will also allow the site owner to automatically swap back to normal Gallery block markup if they switch themes, so non-Bootstrap themes won't break the block.

The downside is, the filter runs after WordPress has already retrieved the content, so you're looking at slightly longer processing times of the final markup. This shouldn't be too big of an issue for most sites, especially if you have some kind of caching set up.

You can find documentation on render_block on the WP Developer site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.