Block Hooks: Apply to Post Content (on frontend) #67272
Draft
+12
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Details to follow. Trac ticket: https://core.trac.wordpress.org/ticket/61074
What?
Apply Block Hooks to Post Content (frontend only).
Why?
We've seen some demand for this. A typical example would be a plugin that provides blocks that can be used in posts and that would like to provide extensibility for those blocks.
Why not?
Note that hooked blocks are inserted on the frontend, but not in the editor. This violates editor/frontend parity, which we've previously taken great care to preserve (when applying Block Hooks gradually to more contexts, such as templates, Navigation blocks, etc).
The alternative -- inserting hooked blocks into post content in the editor -- is also not without problems.
How?
By adding a filter to
the_content
that invokesapply_block_hooks_to_content
.Testing Instructions
You need a block that hooks to an anchor block that's present in your content.
I'm using the Like Button block, which I've modified locally to be inserted
after
thecore/gallery
block:I've created a post with a Gallery block, followed by a paragraph.
When viewed on the frontend, that should cause the Like Button block to be inserted after the gallery (and before the paragraph).
Screenshots or screencast
(Note that the second Like Button that's visible in the screenshot is inserted by this code. If you'd like to remove it, you can comment out that filter, or edit the template to delete the block.