Skip to content

Post Images: add image exclusion via CSS class and filter#47183

Open
kraftbj wants to merge 2 commits intotrunkfrom
related-posts-image-filter
Open

Post Images: add image exclusion via CSS class and filter#47183
kraftbj wants to merge 2 commits intotrunkfrom
related-posts-image-filter

Conversation

@kraftbj
Copy link
Contributor

@kraftbj kraftbj commented Feb 18, 2026

Fixes #47054

Proposed changes:

  • Add jetpack-ignore-thumbnail CSS class support to Jetpack_PostImages — any <img> tag with this class (or any block with this class in "Advanced > Additional CSS class") is skipped during image discovery
  • Add jetpack_postimages_exclude_image filter for programmatic exclusion — developers can hook in to exclude images by URL pattern, dimensions, or other metadata
  • Both mechanisms apply at the Jetpack_PostImages level (from_html and from_blocks), so all consumers benefit: Related Posts, Open Graph tags, and anything else that relies on this class for image selection

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

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

No.

Testing instructions:

  • Create a post with two images and no featured image set
  • Add the CSS class jetpack-ignore-thumbnail to the first image:
    • Block editor: Select the image block → open the "Advanced" panel in the sidebar → add jetpack-ignore-thumbnail to "Additional CSS class(es)"
    • Classic editor: Add class="jetpack-ignore-thumbnail" to the <img> tag in the HTML view
  • Verify that Related Posts (and/or Open Graph meta tags) use the second image instead of the first
  • Alternatively, test the filter by adding this to a plugin or functions.php:
add_filter( 'jetpack_postimages_exclude_image', function( $exclude, $image ) {
    if ( str_contains( $image['src'], 'qr-code' ) ) {
        return true;
    }
    return $exclude;
}, 10, 2 );

Changelog

  • Generate changelog entries for this PR (using AI).

Add two mechanisms to exclude specific images from Jetpack_PostImages
discovery, which powers Related Posts thumbnails, Open Graph tags, and
other features:

- `jetpack-ignore-thumbnail` CSS class: add to any <img> tag in
  classic editor HTML or via the block editor's "Advanced > Additional
  CSS class" panel to skip the image during content scanning.
- `jetpack_postimages_exclude_image` filter: allows programmatic
  exclusion based on the image's URL, dimensions, or other metadata.

Both apply at the Jetpack_PostImages level (from_html and from_blocks),
so all consumers benefit.

See #47054
Copilot AI review requested due to automatic review settings February 18, 2026 03:16
@kraftbj kraftbj added [Status] Needs Review This PR is ready for review. Enhancement labels Feb 18, 2026
@kraftbj kraftbj self-assigned this Feb 18, 2026
@kraftbj kraftbj requested a review from jeherve February 18, 2026 03:16
@github-actions
Copy link
Contributor

github-actions bot commented Feb 18, 2026

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 related-posts-image-filter branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack related-posts-image-filter

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

@github-actions github-actions bot added [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests labels Feb 18, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 18, 2026

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:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: March 3, 2026
    • Code freeze: March 3, 2026

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

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds image exclusion capabilities to Jetpack_PostImages, allowing users to prevent specific images from being selected for Related Posts thumbnails, Open Graph tags, and other features. The implementation provides two exclusion mechanisms: a CSS class (jetpack-ignore-thumbnail) and a programmatic filter (jetpack_postimages_exclude_image).

Changes:

  • Added CSS class-based exclusion (jetpack-ignore-thumbnail) that works in both from_html() and from_blocks() methods
  • Implemented jetpack_postimages_exclude_image filter for programmatic image exclusion with comprehensive documentation
  • Added test coverage for both CSS class exclusion and filter-based exclusion in HTML and block contexts

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
projects/plugins/jetpack/class.jetpack-post-images.php Core implementation of CSS class check and filter hook in from_html() and from_blocks() methods with detailed filter documentation
projects/plugins/jetpack/tests/php/media/Jetpack_PostImages_Test.php Comprehensive test coverage for CSS class exclusion in HTML/blocks and filter-based exclusion
projects/plugins/jetpack/changelog/related-posts-image-filter Changelog entry describing the enhancement

@jp-launch-control
Copy link

jp-launch-control bot commented Feb 18, 2026

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/class.jetpack-post-images.php 333/544 (61.21%) 0.47% 1 ❤️‍🩹

Full summary · PHP report · JS report

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

Labels

Enhancement [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] Needs Review This PR is ready for review. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add image exclusion support to Jetpack_PostImages (CSS class + filter)

1 participant