Visitar URL original
Related Posts: Add the block to block theme templates via block hooks · Issue #53945 · Automattic/jetpack · GitHub
Skip to content

Related Posts: Add the block to block theme templates via block hooks #53945

Open

Description

@jeherve

Impacted plugin

Jetpack

What

Add the Related Posts block to block theme templates automatically with the Block Hooks API, the same way we now add the Sharing Buttons and Like blocks.

How

This is split out from #36382, which covered Sharing, Likes and Related Posts. Sharing buttons and Likes can now be placed before or after the post content from the redesigned sharing settings screen (#53413, #53838), so Related Posts is the one left.

Where things are today

On classic themes, Related Posts is appended to the post content when the module is on (Jetpack_RelatedPosts::filter_add_target_to_dom()). On block themes, that function bails out, and has since Jetpack 11.1. Site owners have to open the Site Editor and add the Related Posts block to their Single template themselves. That's easy to miss: on a block theme, the "Show related content after posts" toggle in Jetpack settings doesn't add anything to your posts (see #27200 and #11021).

What I have in mind

  • Hook jetpack/related-posts after core/post-content in the templates and patterns that show a single post, when the Related Posts module is active. Hooked_Blocks in projects/packages/sharing-likes/src/hooked-blocks/ is the pattern to follow; Newsletter's Jetpack_Subscription_Site does something similar for the Subscribe block.
  • Pass the existing module options (headline, thumbnails, date, context, layout, number of posts) as block attributes, so the hooked block looks like what the site showed on a classic theme.
  • Decide where the switch lives. We could reuse the existing "Show related content after posts" toggle, or offer placement cards like the ones on the sharing settings screen.

Things we ran into on Sharing & Likes

  • Block Hooks don't check whether the block is already in the template. Sites that already added Related Posts to their Single template by hand would end up with two. Hooked_Blocks::already_placed() handles this for Sharing and Likes, including patterns that reach the filter with only a filePath.
  • Once someone saves a template in the Site Editor, hooked blocks become ordinary blocks. Turning the setting off afterwards won't remove them.
  • A lot of themes put the post content in a pattern, so the hook's context can be a pattern array rather than a template.
  • The single template also serves custom post types. Related Posts already runs on any singular view on block themes (enabled_for_request()), so we'll need to decide whether the hooked block follows that or sticks to posts.

Related: #51056 (the block rendering empty on a block theme) would become a lot more visible once the block is added for people, so I'd fix that one first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

EnhancementChanges to an existing feature — removing, adding, or changing parts of itJetpack Agent Backlog[Feature] Related Posts[Focus] FSEIssues related to the site editor / Full Site Editing / FSE feature in Gutenberg[Pri] Normal

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions