WordPress / developer-blog-content

In this GitHub space, WordPress team coordinate content to be published on the Developer Blog. Discussion and montly meetings (first Thu) in WP Slack #core-dev-blog
36 stars 2 forks source link

Building dynamic attachment templates in block themes #199

Closed justintadlock closed 5 months ago

justintadlock commented 6 months ago

Discussed in https://github.com/WordPress/developer-blog-content/discussions/192

Originally posted by **justintadlock** December 19, 2023 Block themes support `attachment.html` templates, but WordPress is not very "smart" about handling the output of media for attachment views. At the moment, there are no dynamic Image, Audio, and Video blocks that automatically pick up the current attachment media file on the front end. Yes, attachments views are now disabled by default for new WP installs (as of 6.4?), but there are still many use cases where attachment templates are needed. Typically, I don't like to propose topics for the Dev Blog that are outside the standards of theme dev, but this is one of those situations that requires a bit of hacking to make things work as well as they did in classic. That's unfortunate, but this is a missing piece of block theme dev with no instructions/docs (AFAIK) on the best way to handle this. I'm proposing a tutorial on how to filter the Post Content block for attachment views to insert a sort of dynamic template partial/pattern that allows theme authors to control the output of attachment pages. I have a working example in my own theme that I could break down for this tutorial: - Filter: https://github.com/x3p0-dev/x3p0-ideas/blob/da7b5abc87dd05312b58d1ac49cf699ac9ef8800/src/Blocks.php#L31-L115 - Partials: https://github.com/x3p0-dev/x3p0-ideas/tree/da7b5abc87dd05312b58d1ac49cf699ac9ef8800/public/partials
justintadlock commented 6 months ago

Example repository: https://github.com/justintadlock/dynamic-attachments

I'll probably begin writing the tutorial on this one next week.

justintadlock commented 5 months ago

I have started writing this here: https://docs.google.com/document/d/1VRHL7Pubz90UH14pdNBxI4HYmpxxnepDX9grflCwm64/edit?usp=sharing

justintadlock commented 5 months ago

This one is ready for its first review: https://docs.google.com/document/d/1VRHL7Pubz90UH14pdNBxI4HYmpxxnepDX9grflCwm64/edit?usp=sharing

bph commented 5 months ago

1st review done. @justintadlock I left a few minor comments, mostly tone and filler. I appreciate this post very much, as it demonstrates clearly, that PHP can be used to while creating block themes and prior solutions can be integrated.

justintadlock commented 5 months ago

Thank you for the review, @bph. As always, great feedback.

I had a couple of in-comment questions, but I'm moving this to needs 2nd review since the other edits have been resolved.

thetinyl commented 5 months ago

@justintadlock I've just finished a 2nd review of your piece. Some minor copy edits and small nitpicks to consider. Looks good otherwise. 👍

justintadlock commented 5 months ago

Social copy:

From the WordPress Developer Blog: learn how to dynamically customize media output in block themes:

https://developer.wordpress.org/news/2024/01/22/building-dynamic-block-based-attachment-templates-in-themes/

ndiego commented 5 months ago

Social post scheduled.