Lazyload SVG placeholder runs uncached attachment lookups with JSON-escaped URLs on REST responses
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
- Domain
- backend, performance
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
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)
- Enable Lazyload and "Enable generic placeholder color".
- Add an attachment image without width/height attributes to a post.
- 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
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Codeinwp/optimole-wp
-
customer report feature-request-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Codeinwp/optimole-wp#1239 ·
Maintainers usually reply within 1 day
-
Page profiler never stores above-the-fold background images: `b` is always sent with empty selector mapsPossibly taken @girishpanchal30 claimed this 19 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
Codeinwp/optimole-wp#1216 · 5 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
Throttle and optimize `/wp-json/optml/v1/optimizations` REST API endpoint execution and validationOpencustomer report feature-request-triage
Difficulty 3/5 1-2 days Newbie friendliness 45/100
Codeinwp/optimole-wp#1250 ·
Maintainers usually reply within 1 day
-
Cache `attachment_url_to_postid()` lookups in `Optml_Lazyload_Replacer` to reduce database overheadOpencustomer report feature-request-triage
Difficulty 3/5 1-2 days Newbie friendliness 65/100
Codeinwp/optimole-wp#1249 · 1 comment ·
Maintainers usually reply within 1 day
-
customer report feature-request-triage
Difficulty 5/5 Over a week Newbie friendliness 35/100
Codeinwp/optimole-wp#1231 ·
Maintainers usually reply within 1 day
All issues in Codeinwp/optimole-wp
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
HE-Arc/le-panier-du-coin#49 ·
Maintainers usually reply within 1 day
-
15.0
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
bug: juros do parcelamento travados em 1x no gateway Rede Débito e Crédito 3DS (woo-rede 5.6.0)Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
LinkNacional/woo-rede#367 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 75/100
Maintainers usually reply within 1 day