Lazyload SVG placeholder runs uncached attachment lookups with JSON-escaped URLs on REST responses

Open Beginner friendly
#1,253 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php, wordpress

Research direction

Start in inc/tag_replacer.php around line 304, where the raw img_url is passed to optml_tag_replace, then read Optml_Lazyload_Replacer::get_svg_for() and its is_file() / attachment_url_to_post_id() calls. Reproduce by enabling Lazyload and the generic placeholder color, then requesting /wp-json/wp/v2/posts/<id> logged out while watching wp_postmeta queries. Done means the REST placeholder has the real dimensions and no _wp_attached_file lookups run against the JSON-escaped URL.

Written by the indexing model from the issue text.

Description

customer report

Reported in: https://secure.helpscout.net/conversation/3524196151/501323

On REST requests, Optml_Manager::process_template_redirect_content() buffers the whole JSON body, and Optml_Tag_Replacer passes the raw escaped img_url (https:\/\/...) to the optml_tag_replace filter (inc/tag_replacer.php:304). Optml_Lazyload_Replacer::get_svg_for() then calls is_file() and attachment_url_to_post_id() with that escaped URL. The file check fails and all 4 attachment_url_to_postid() variants miss (the www variant is also a no-op, since :// is not present in an escaped URL). ID 0 is cached for 10 minutes, so the lookups repeat after that.

Steps to reproduce (4.2.20)
  1. Enable Lazyload and "Enable generic placeholder color".
  2. Add an attachment image without width/height attributes to a post.
  3. Request /wp-json/wp/v2/posts/<id> while logged out.
Expected

The placeholder uses the real dimensions, as it does on the front end.

Actual

A 100% x 100% SVG placeholder, plus 4 wp_postmeta queries per image matching the escaped URL:

SELECT post_id, meta_value FROM wp_postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = 'http:\\/\\/localhost:8955\\/wp-content\\/uploads\\/2026\\/11\\/chart.jpg'
SELECT post_id, meta_value FROM wp_postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = 'http:\\/\\/localhost:8955\\/wp-content\\/uploads\\/2026\\/11\\/chart-scaled.jpg'
(same two again for the www variant)
Request Placeholder _wp_attached_file queries
Front end 2200 x 360 (correct) 0
REST 100% x 100% (fallback) 4 per image
REST, generic placeholder disabled blurred preview 0

On the reporter's site (large wp_postmeta table, Memcached object cache) one REST request for a custom post type made 29 wp_postmeta queries taking 6.25 s of a 7.98 s transaction, traced in New Relic through Optml_Lazyload_Replacer::get_svg_for > attachment_url_to_post_id.

Workaround

Disable "Enable generic placeholder color" (Settings > Advanced > Lazyload).

Suggested fix

Unslash the URL before get_svg_for() and the attachment lookup. Use the wp-image-<ID> class when it is present, before any URL lookup.

Related: #1249

Dominant language
PHP
Stars
74
Forks
14
Avg merge
1d 2h
Merged PRs (30d)
35

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Codeinwp/optimole-wp

All issues in Codeinwp/optimole-wp

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.