Skip to content

Background image with a gradient is not lazy-loaded on WordPress 7.1 (Quote, Pullquote, Verse, Post Content, Accordion) #1203

Closed

Description

@kushh23

Description

WordPress 7.1 adds background.gradient support to the Group, Quote, Pullquote, Verse, Post Content and Accordion blocks. The default theme.json turns it on, and the editor now saves the gradient to style.background.gradient. When a block has both a gradient and a background image, core writes them as one value:

background-image:linear-gradient(...), url('https://.../hero.jpg');background-size:cover;

Optimole's default background lazy-load watchers only look for background-image:url( / background:url(, plus a class rule for Cover and Group. The combined value starts with the gradient, so on Quote, Pullquote, Verse, Post Content and Accordion no watcher matches. The background-image: none !important hide rule is never applied and the image downloads on page load, even far below the fold.

On WordPress 7.0.x the same design is lazy-loaded. There the gradient comes from the Color panel as background:linear-gradient(...) and the image as a separate background-image:url(...), which matches [style*="background-image:url("]. Group is still lazy-loaded on 7.1 because of its class-based watcher.

Expected: a block with a gradient and a background image is lazy-loaded the same way as a block with only a background image.

Step-by-step reproduction instructions

  1. WordPress 7.1.3, a block theme (e.g. Twenty Twenty-Five), Optimole 4.2.17 connected. Enable lazy load and background lazy load:
    wp optimole setting update lazyload on
    wp optimole setting update bg_replacer on
    
  2. Upload a JPG and keep its URL: IMG=$(wp media import ./hero.jpg --porcelain=url)
  3. Create a page with a tall spacer, then two Quote blocks. The first has only the image. The second has the image and a gradient:
    wp post create --post_type=page --post_status=publish --post_title="BG gradient test" --post_content="<!-- wp:spacer {\"height\":\"2000px\"} --><div style=\"height:2000px\" aria-hidden=\"true\" class=\"wp-block-spacer\"></div><!-- /wp:spacer -->
    <!-- wp:quote {\"className\":\"img-only\",\"style\":{\"background\":{\"backgroundImage\":{\"url\":\"$IMG\",\"source\":\"file\"}}}} --><blockquote class=\"wp-block-quote img-only\"><!-- wp:paragraph --><p>Image only</p><!-- /wp:paragraph --></blockquote><!-- /wp:quote -->
    <!-- wp:quote {\"className\":\"img-gradient\",\"style\":{\"background\":{\"backgroundImage\":{\"url\":\"$IMG\",\"source\":\"file\"},\"gradient\":\"linear-gradient(135deg,rgb(6,147,227) 0%,rgb(155,81,224) 100%)\"}}} --><blockquote class=\"wp-block-quote img-gradient\"><!-- wp:paragraph --><p>Image + gradient</p><!-- /wp:paragraph --></blockquote><!-- /wp:quote -->"
    
    (In the editor this is: Quote block > Styles > Background > pick an image and a gradient.)
  4. Open the page in a private window with DevTools open on Network > Img. Do not scroll. The hero image is requested right away. With only the first Quote on the page, it is not requested until you scroll.
  5. In the console:
    document.querySelector('.img-only').matches(optimoleData.backgroundLazySelectors)      // true
    document.querySelector('.img-gradient').matches(optimoleData.backgroundLazySelectors)  // false
    
  6. Optional: repeat on WordPress 7.0.3 with a gradient set from Color > Background and the same image. The block is lazy-loaded.

A PHP CLI check that runs the real 7.0.3 and 7.1.3 style engine and tests the output against the watchers in inc/lazyload_replacer.php:127-132 gives:

7.0.3 color gradient + image:  background:linear-gradient(...);background-image:url('.../hero.jpg');background-size:cover;
  wp-block-quote  lazy-loaded: YES via [style*="background-image:url("]
7.1.3 background gradient + image:  background-image:linear-gradient(...), url('.../hero.jpg');background-size:cover;
  wp-block-quote  lazy-loaded: NO (no default watcher matches)
  wp-block-group  lazy-loaded: YES via [class*="wp-block-group"][style*="background-image"]

A preset gradient behaves the same way: background-image:var(--wp--preset--gradient--...), url(...).

Screenshots, screen recording, code snippet or Help Scout ticket

Default watchers, https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/lazyload_replacer.php#L127-L132

$default_watchers = [
	'[class*="wp-block-cover"][style*="background-image"]',
	'[style*="background-image:url("]', '[style*="background-image: url("]',
	'[style*="background:url("]', '[style*="background: url("]',
	'[class*="wp-block-group"][style*="background-image"]',
];

These selectors are the only thing that decides what gets hidden and lazy-loaded: https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/admin.php#L998-L1009 (CSS hide rule) and https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/admin.php#L614 (backgroundLazySelectors for the lazy-load script).

Core 7.1 merges the gradient and the image into one background-image: https://github.com/WordPress/WordPress/blob/7.1.3/wp-includes/style-engine/class-wp-style-engine.php#L523-L529

if ( isset( $css_declarations['background-image'] ) && isset( $parsed_styles['declarations']['background-image'] ) ) {
	$css_declarations['background-image'] = $css_declarations['background-image'] . ', ' . $parsed_styles['declarations']['background-image'];
}

The block support passes the gradient on: https://github.com/WordPress/WordPress/blob/7.1.3/wp-includes/block-supports/background.php#L94-L98. The block supports are in https://github.com/WordPress/WordPress/blob/7.1.3/wp-includes/blocks/quote/block.json#L33-L41 (also pullquote, verse, post-content, accordion, group).

Workaround: add the block classes (e.g. .wp-block-quote.has-background) to the background lazy-load selectors setting. A possible fix is a default watcher that matches an image anywhere in the value, e.g. [style*="background-image"][style*="url("].

Environment info

Optimole 4.2.17 (master has the same watchers). WordPress 7.1.0 and later (checked against 7.1.3); not affected on 7.0.x. PHP version does not matter (checked with PHP 8.4).

Is the issue you are reporting a regression

No. This is not an Optimole regression. The same block design is lazy-loaded on WordPress 7.0.x, and the change comes from the WordPress 7.1 core output. #717 covered the same kind of gap for Group backgrounds in WordPress 6.4.

Found while writing the WordPress compatibility checks for Optimole.

Activity

  1. added
    bugThis label could be used to identify issues that are caused by a defect in the product.
    on Oct 28, 2026
  2. added theissue type on Oct 28, 2026
  3. girishpanchal30 commented on Oct 29, 2026

    @girishpanchal30
    Contributor

    I have fixed it with PR #1212

  4. milicavs commented on Oct 29, 2026

    @milicavs

    🧪 QA steps and test links

    On WordPress 7.1, a Quote, Pullquote, Verse, Post Content or Accordion block that has both a background gradient and a background image is not lazy-loaded by Optimole. Its image downloads as soon as the page opens, even far below the fold. The same block with only an image is lazy-loaded.

    Requires: WordPress 7.1 or later and a block theme (Twenty Twenty-Five).

    Link Build Bot check
    Reproduce the bug Optimole 4.2.17 (released), WordPress 7.1.4 ✅ Reproduced: the "Image only" Quote matches Optimole's background lazy-load selectors, the "Image + gradient" Quote doesn't
    Verify the fix PR #1212 build beefea0 (Plugins shows 4.2.17) ✅ Fixed: both Quotes match, and the hide-until-scrolled rule now covers the gradient + image block

    In the links:

    • Steps 1 to 3 are already done, and the links open on the "BG gradient test" page.
    • Optimole uses a stand-in connection (no account), so images point at a fake Optimole address and the Network check in step 5 shows nothing useful. Use step 6 instead: in DevTools' Console, first pick the page's frame (the scope: one) in the context menu.
    • Tools → QA Logs shows debug.log.

    Reproduce on Optimole 4.2.17 (connected, Lazy load and background lazy load on):

    1. Pages → Add Page, title "BG gradient test". Add a Spacer block, height 2000px.
    2. Below it, add a Quote block with text "Image only". In Styles → Background, pick an image. Under Advanced, give it the CSS class img-only.
    3. Add a second Quote, "Image + gradient". In Styles → Background, pick the same image and a gradient. CSS class img-gradient. Publish.
    4. Open the page in a private window with DevTools open on Network → Img. Don't scroll.
    5. Look for the image's request.
    6. In the Console run document.querySelector('.img-gradient').matches(optimoleData.backgroundLazySelectors).
    • Bug: the image is requested right away, and the console prints false (the same check for .img-only prints true).
    • Fixed: the image is only requested when you scroll down to the Quotes, and the console prints true.
    Edge cases
    • Pullquote, Verse and Post Content blocks with a gradient and an image: lazy-loaded with the fix.
    • A preset gradient (picked from the palette) with an image: lazy-loaded with the fix.
    • A block with only a gradient and no image: not watched; the gradient shows at once on both builds.
    • Group and Cover blocks with a gradient and an image: lazy-loaded on both builds, as before.
    • WordPress 7.0.x with a Color → Background gradient and an image: lazy-loaded on both builds.

    Fix: #1212 (open)

  5. pirate-bot commented on Oct 29, 2026

    @pirate-bot
    Collaborator

    QA agent — 🟢 Passing checks · Gradient background images now wait for scrolling on WordPress 7.1.

    The base build downloaded all ten gradient images before scrolling; the candidate requested none. All five affected block types loaded their backgrounds on scroll and kept their gradients. Image-only controls and a gradient-only Quote also behaved correctly. The run used a seeded connection and local JPG delivery with Optimole’s real lazy-load script.

    Worth a human look

    1. Check custom optml_lazyload_bg_selectors callbacks outside this repository for positional array access; those external implementations were not available.
    2. Exercise the separate missing-image scenario from the code review’s Fatal error with list() assignment in lazyload_replacer.php on PHP 8.2+ when handling missing images #1034 note; this selector-focused run used valid JPG backgrounds.
    Checks (3 passed)
    • ✅ On WordPress 7.1, below-the-fold blocks with an image and a custom or preset background gradient do not request their background images before scrolling, just like image-only blocks.
    • ✅ Scrolling to each deferred background loads and displays its image while preserving the custom or preset gradient; image-only controls still load normally.
    • ✅ A gradient-only Quote remains visible without waiting for lazy loading and does not match the image lazy-load watchers.

    Full report, evidence and session trail

  6. pirate-bot commented on Nov 2, 2026

    @pirate-bot
    Collaborator

    🎉 This issue has been resolved in version 4.2.18 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

  7. added
    releasedIndicate that an issue has been resolved and released in a particular version of the product.
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugThis label could be used to identify issues that are caused by a defect in the product.releasedIndicate that an issue has been resolved and released in a particular version of the product.

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions