Skip to content

Sharing & Likes: Hook the Sharing Buttons and Like blocks before or after the post content - #53413

Merged
jeherve merged 15 commits into
trunkfrom
add/sharing-likes-template-placements
Oct 16, 2026
Merged

jeherve merged 15 commits into
trunkfrom
add/sharing-likes-template-placements

Conversation

@jeherve

@jeherve jeherve commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Fixes CM-972

Proposed changes

On block themes, the Sharing settings send owners to the Site Editor to add the Sharing Buttons and Like blocks themselves. This adds the PHP half of doing it for them: two options say where each block goes, and the Block Hooks API puts it there, before or after the post content of single posts and pages.

  • jetpack_sharing_buttons_auto_add and jetpack_likes_auto_add each hold a list: before_content or after_content. Template_Placements owns both and is their only writer; the REST settings (CM-975) will save through it. The Sharing option reuses the name the plugin's unfinished auto-add code read as a boolean, so a truthy old value still means "after the content".
  • Hooked_Blocks hooks jetpack/sharing-buttons and jetpack/like next to core/post-content in the single, single-post*, page, page-* and singular templates, in theme custom templates declared for posts or pages, and in patterns declared for those template types. Template parts, post content and custom templates that declare no post types get nothing.
  • Each block is wrapped in a group that copies the post content's alignment and layout, so it lines up with it.
  • The Sharing Buttons block carries over the site's legacy services and button style where the block supports them, with labels, and falls back to Facebook, X and Mastodon. Simple switches to the block by removing every service, so Feature_Actions keeps a copy first, and the block reads from it.
  • The blocks are only hooked where they can load and the legacy buttons are gone. That means a block theme, and Jetpack's blocks loading as Jetpack_Gutenberg::should_load() decides: the Blocks module with a ready connection or offline mode, then the jetpack_gutenberg filter. On Simple, the first part always holds. On top of that, the Sharing Buttons block needs the Sharing module off or every service removed; the Like block needs the Likes module off (or switched to the block on Simple) on a site that can show Likes at all.
  • The post being viewed can keep a block out too, as it would the legacy buttons: its own Sharing or Likes switch, or a password for the Like block. On Simple, a post that switched Likes on keeps the legacy Like button, so it doesn't get the block on top of it.
  • After the content, the order is Sharing Buttons, Like, then Newsletter's Subscribe block, as with the legacy buttons.
  • On Simple, sharing.php only sets the package up on admin and API requests, so the Sharing Buttons block's registration sets up the hooked blocks on Simple as well. Without it, they showed in the Site Editor but never on live posts.
  • The unfinished auto-add code in extensions/blocks/sharing-button/sharing-button.php goes. Nothing ever wrote its option, so it never ran.

There's no UI yet: the cards that set these options come in CM-976, on the React screen, which is still opt-in. Until then, WP-CLI is the only way in :)

Screenshots

Twenty Twenty-Five, with the Like block set before the content and the Sharing Buttons block after it:

A single post with the Like block above the post content and the Sharing Buttons block below it

The same blocks in the Single Posts template in the Site Editor, where they can be moved or removed:

The Single Posts template in the Site Editor, with the Like block above the Content block and the Sharing Buttons block below it

Both blocks after the content, with Newsletter's "Subscribe block at the end of each post" on:

A single post with the Sharing Buttons, Like and Subscribe blocks below the post content, in that order

Rationale

One side of the content per block. Core can't hook the same block type both before and after one anchor in a template. Theme templates, saved templates and patterns are built with insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata(), which records the type in the anchor's ignoredHookedBlocks right after the before pass, so the after pass skips it. Each option therefore holds at most one side; given both, it keeps after_content. Two different blocks on different sides work fine.

No block registry check. like and sharing-buttons are lazy-loaded on the front end since #49840: they register when they first render, after core has built the template. Like Subscribe's own placement, the callbacks check the conditions the blocks load under instead, and the lazy loader registers the inserted blocks as they render. Both blocks stay lazy. The trade-off: a site that drops one of these two with jetpack_set_available_extensions still gets them hooked.

Filters on every request, options read in the callbacks. WordPress.com REST requests switch blogs after init, which is why Newsletter defers its setup there. Reading everything inside the callbacks avoids that, and nothing is memoised per site. The hooked_block_types callback runs at priority 5, because Simple sets this package up after Subscribe registers its own filter at 10.

No blocks in custom templates that declare no post types. A template made from the post editor doesn't say which post types it's for, so only the request could tell. The front end would add the blocks while a post is viewed, but the Site Editor never views one, so the owner could neither see them there nor remove them. Those templates get nothing instead, as with Subscribe, which only matches the single template. Posts on such a template lose the buttons, but the front end and the Site Editor agree.

Known gap. On a theme that puts core/post-content in a pattern without declaring its template types, the blocks show on the front end but not in the Site Editor, and saving that template there turns them off for good. The default themes don't do this. The package's AGENTS.md documents it, along with the core behaviours above.

Related product discussion/links

Does this pull request change what data or activity we track or use?

No.

Testing instructions

On a connected Jetpack site (a JN site works) running Twenty Twenty-Five, with the Sharing and Likes modules off:

  • Set the options:
    wp option update jetpack_sharing_buttons_auto_add '["after_content"]' --format=json
    wp option update jetpack_likes_auto_add '["before_content"]' --format=json
    
  • Open a single post and a page. The Like block should show above the content and the Sharing Buttons block below it.
  • Set a static front page under Settings > Reading and open the home page. It gets the blocks too.
  • Open the Site Editor, then the Single Posts and Pages templates. The same blocks should be there, valid and editable.
  • Open the blog home and an archive, and scroll to the "More posts" list at the bottom of a single post. None of them should get the blocks.
  • Set both options to '["after_content"]', and turn on Jetpack > Newsletter > "Subscribe block at the end of each post". After the content, the order should be Sharing Buttons, Like, Subscribe.
  • Turn the Sharing module on. The Sharing Buttons block should go from both the front end and the Site Editor, as long as you haven't saved the template there.
  • Remove one of the hooked blocks in the Site Editor and save. It should stay removed.
  • Turn sharing off on one post with wp post meta update <post-id> sharing_disabled 1, and Likes off on another with wp post meta update <post-id> switch_like_status 0. Each should lose that block only.
  • In the post editor, open a post's Template panel and create a new template for it. That post should get neither block, on the front end or in the Site Editor.
  • On a WordPress.com Simple sandbox with this branch, on a block theme: enable a few sharing services (LinkedIn and Reddit, say), use "Switch to the Sharing Buttons block" in Settings > Sharing, and set jetpack_sharing_buttons_auto_add as above. A single post should show the Sharing Buttons block on the front end, with LinkedIn and Reddit.
  • Lazy loading still works: add this mu-plugin, and open a page that doesn't render either block (with both options emptied, for example). wp-content/debug.log should list neither file:
    add_action( 'shutdown', function () {
    	foreach ( get_included_files() as $file ) {
    		if ( preg_match( '#extensions/blocks/(like/like|sharing-buttons/sharing-buttons)\.php$#', $file ) ) {
    			error_log( 'Loaded: ' . $file );
    		}
    	}
    } );

Owners will choose where the Sharing Buttons and Like blocks go in
block theme templates: before or after the post content of single
posts and pages. Template_Placements owns the two options holding
that choice, and is the one writer the upcoming REST settings will
save through.

jetpack_sharing_buttons_auto_add keeps the name the plugin's
unfinished auto-add code read as a boolean, so a truthy scalar still
means "after the content". jetpack_likes_auto_add is new.

Both hold a list, leaving room for placements other than the post
content later, but at most one side of it: core inserts a block
hooked both before and after the same anchor only before it, because
the "before" pass adds the type to the anchor's ignoredHookedBlocks
and the "after" pass then skips it. "After" wins, as the legacy value
meant.
…post content

Today the Sharing settings send owners of block themes to the Site
Editor to add the blocks themselves, leaving them to find the right
template and the right spot in it. With the Block Hooks API, the
blocks appear wherever Template_Placements asks for them, as real
blocks the owner can move or remove in the Site Editor.

The blocks go before or after core/post-content in the single post
and page templates (single, single-post*, page, page-*, singular, and
theme custom templates declared for posts or pages), and in patterns
declared for those template types. Other patterns fall back to the
current request, as Newsletter's Subscribe placement does.

The hooks apply only where both blocks can load and the legacy
buttons are gone, so visitors never get both. They don't ask the
block registry: like and sharing-buttons are registered lazily on
first render on the front end, after core has built the template.
Environment::jetpack_blocks_load() checks the conditions
Jetpack_Gutenberg::should_load() uses instead, minus its filter.

Sharing Buttons carry over the site's visible legacy services and
button style where the block supports them, with labels, so the
default style doesn't print raw service slugs.

The hooked_block_types filter runs at priority 5 so both blocks stay
ahead of Subscribe after the content on Simple too, where this
package is set up after Subscribe registers its filter.
The block file added the Sharing Buttons block after the post content
of single and page templates when jetpack_sharing_buttons_auto_add was
set. Nothing ever wrote that option, so the code never ran, and the
Sharing & Likes package now hooks the block (and the Like block) from
the same option, read as a list.

Leaving both in place would hook the block twice once the option is
set, and the old Simple check read global['show'] in a way that
ignores the "Switch to the block" route.
@jeherve jeherve added Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] Needs Review This PR is ready for review. [Package] Sharing Likes labels Oct 9, 2026
@jeherve jeherve self-assigned this Oct 9, 2026
@github-actions github-actions Bot added [Block] Sharing Button [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests Docs labels Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the add/sharing-likes-template-placements branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/sharing-likes-template-placements

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@jp-launch-control

jp-launch-control Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 4 files.

File Coverage Δ% Δ Uncovered
projects/packages/sharing-likes/src/settings/class-feature-actions.php 0/22 (0.00%) 0.00% 1 ❤️‍🩹
projects/packages/sharing-likes/src/class-initializer.php 7/7 (100.00%) 0.00% 0 💚
projects/packages/sharing-likes/src/settings/class-environment.php 27/41 (65.85%) 4.74% 0 💚
projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php 6/112 (5.36%) 5.36% -82 💚

4 files are newly checked for coverage.

File Coverage
projects/packages/sharing-likes/src/hooked-blocks/class-template-placements.php 18/20 (90.00%) 💚
projects/packages/sharing-likes/src/hooked-blocks/class-sharing-buttons-markup.php 68/70 (97.14%) 💚
projects/packages/sharing-likes/src/hooked-blocks/class-hooked-blocks.php 106/106 (100.00%) 💚
projects/packages/sharing-likes/src/class-block-names.php 0/0 (—%) 🤷

Full summary · PHP report · JS report

jeherve added 12 commits October 9, 2026 15:31
The removed code never ran, since nothing ever wrote the option it
read, so site owners have nothing to read about. The changelog check
still wants a file for every project a PR touches, so the entry stays
with a comment and no text, which keeps it out of CHANGELOG.md and
readme.txt.
It described what the template placement gate does not check, which
reads as more confusing than helpful in a guide. The reason the gate
skips the block registry stays where the gate lives, in the docblock of
Hooked_Blocks::blocks_can_load().
Review follow-ups for the template placements:

- Skip a block the template, pattern or viewed post already holds, so
  sites that added Sharing Buttons by hand don't get a second copy.
  Theme patterns reach the filter before core reads their file, so read
  it there.
- On the front end, give singular requests for other post types
  nothing: `single` and `singular` serve every post type.
- Match `single-post-*` only when no `post-*` post type owns the slug,
  match pattern template types like template slugs, and place the
  blocks in custom templates that declare no post types while a post or
  page is viewed.
- Carry over the services behind "More" when none are visible, and
  leave the `official` style out: the block accepts it but has no
  styles for it.
The package spelled out `jetpack/sharing-buttons`, `jetpack/sharing-button`
and `jetpack/like` in Hooked_Blocks, Sharing_Buttons_Markup, Environment
and across the tests. A single Block_Names class now holds them, so every
caller reads the same constant.
…blocks

The template placements only hook the Sharing Buttons and Like blocks
into single views of posts and pages. A site that wants them on a custom
post type had no way in: its `single-{type}` template didn't match, and
the front-end guard hid the blocks from its views served by `single`.

jetpack_sharing_likes_template_placement_post_types returns the list,
posts and pages by default. It drives the front-end guard, which
`single-{type}` (and one post's `single-{type}-{slug}`) templates count,
custom templates declared for those types, and the request-time fallback
for patterns and for custom templates that declare no post types.
`page` templates only count while pages are listed, and `single` only
while a type other than pages is. An empty list places nothing, and a
value that isn't an array falls back to the default.
Environment::jetpack_blocks_load() copied Jetpack_Gutenberg::should_load()
but checked the raw connection and skipped its jetpack_gutenberg filter.
It said yes on two kinds of sites where no Jetpack block registers:

- a site that turns the blocks off with that filter;
- an Atomic site whose connection has no owner, where wpcomsh filters
  jetpack_is_connection_ready off.

Both got an empty group, and for sharing an empty list, where the
buttons should be, plus "unsupported block" placeholders in the Site
Editor. Apply both filters as the plugin does. The jetpack_gutenberg
filter can turn the blocks on too, so it now does here as well.
…nt end

On WordPress.com Simple, Initializer::init() only ran from
sharing_admin_init() in modules/sharedaddy/sharing.php. wpcom loads
that file through admin-plugins, which only load for wp-admin and
public-api requests, so on a page view the Hooked_Blocks filters were
never registered. The Site Editor showed the hooked blocks, but live
posts never got them until the owner saved the template.

The Sharing Buttons block is registered eagerly wherever Jetpack's
blocks load, the same condition the placements need, so register the
filters from there on Simple. Self-hosted and Atomic sites keep getting
them from load-jetpack.php, and registering them twice is harmless.
…ocks

Review follow-ups for the template placements:

- Keep a block out of a post that keeps it out of the legacy buttons:
  its own Sharing or Likes switch, and a password for the Like block.
  On Simple, a post that switched Likes on still shows the legacy
  button after Likes go off sitewide, so it got two Like buttons;
  leave the block out there.
- Copy the post content's wide or full alignment to the wrapper group,
  with the class the group saves. Without it, designs like Twenty
  Twenty-Five's left-aligned single post put the buttons in a centred
  column about 350px from the text.
- Once the front end has picked a template, require a singular view of
  a listed post type, so a pattern typed for both archives and single
  posts no longer adds the blocks to every post in an archive.
- Give `single-{type}-{slug}` to the longest post type name that fits.
  `single-post-review-great-book` belonged to `post` when a
  `post-review` post type existed, so the Site Editor showed the blocks
  in that post-review's template.
- Test the Like block's wrapper and the `twitter` alias, and make the
  posts-and-pages fallback row fail when the fallback is gone.
…he block

Simple has no Sharing module to turn off, so Feature_Actions switches
it to the block by emptying both sharing-services lists. The hooked
Sharing Buttons block builds its buttons from those lists, so on
Simple it always found none and fell back to Facebook, X and Mastodon,
whatever the site had set up.

Save a copy of the lists, not autoloaded, before emptying them, and
read it when the live lists give the block nothing. A site that never
saved its services keeps no copy and still gets the block's defaults.
A template made from the post editor lists no post types, so only the
request could say whether it served one of ours. The front end added
the blocks while a post was viewed, but the Site Editor's REST request
never views one, so the owner could neither see the blocks there nor
remove them: core only records a removed hooked block it inserted.

Give those templates nothing, as Newsletter's Subscribe block does by
matching only the `single` template. Posts on such a template lose the
buttons, but the front end and the Site Editor always agree. Theme
custom templates that list their post types are unchanged.
@jeherve
jeherve merged commit 4a651fc into trunk Oct 16, 2026
88 of 89 checks passed
@jeherve
jeherve deleted the add/sharing-likes-template-placements branch October 16, 2026 16:19
@github-actions github-actions Bot removed the [Status] Needs Review This PR is ready for review. label Oct 16, 2026
@github-actions github-actions Bot added this to the jetpack/16.4 milestone Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Sharing Button Docs Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Package] Sharing Likes [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] UI Changes Add this to PRs that change the UI so documentation can be updated. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant