Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Hooks: Apply to Post Content (on frontend) #67272

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Nov 25, 2024

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 invokes apply_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 the core/gallery block:

diff --git a/public/block.json b/public/block.json
index 4bb03d0..59873d1 100644
--- a/public/block.json
+++ b/public/block.json
@@ -18,6 +18,6 @@
        "render": "file:./render.php",
        "viewScript": "file:./view.js",
        "blockHooks": {
-               "core/comment-template": "lastChild"
+               "core/gallery": "after"
        }
 }

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

image

(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.

// The `the_content` filter does not provide the post that the content is coming from.
// However, we can infer it by calling `get_post()`, which will return the current post
// if no post ID is provided.
return apply_block_hooks_to_content( $content, get_post(), 'insert_hooked_blocks' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As simple as that? apply_block_hooks_to_content util is really powerful at this point. What risks do you see with this approach? What if there are synced patterns inside the rendered content? I assume they all are annotated with block hooks to ignore at this point, so double processing should be a non-issue if we ignore the additional CPU usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was also really happy when I saw how straight-forward this was. apply_block_hooks_to_content was introduced by @tjcafferkey, and it really is a great high-level util to insert Block Hooks into a piece of block markup 😄

What risks do you see with this approach? What if there are synced patterns inside the rendered content? I assume they all are annotated with block hooks to ignore at this point, so double processing should be a non-issue if we ignore the additional CPU usage.

Yeah, intuitively I'd say this shouldn't duplicate any hooked blocks, but I do want to give it some good smoke testing to be sure.

@ockham
Copy link
Contributor Author

ockham commented Nov 26, 2024

I've filed a backport (WordPress/wordpress-develop#7889).

I'm also experimenting with an alternative in WordPress/wordpress-develop#7898 (easier to do in Core than in GB), which will insert hooked blocks both on the frontend and in the editor. Maybe we can get that to work after all.

@ockham ockham force-pushed the update/apply-block-hooks-to-post-content branch from 5b5d0e8 to 9931b82 Compare December 2, 2024 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block hooks [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants