Cache `attachment_url_to_postid()` lookups in `Optml_Lazyload_Replacer` to reduce database overhead
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 65/100
- Issue type
- Refactor
- Clarity
- Mostly clear
- Activity status
- Active
- Domain
- performance
Research direction
Start in the Optml_Lazyload_Replacer class (under the plugin's inc/ directory) and find every call site of attachment_url_to_postid() in its replacement pipeline, including the path used for /wp-json/wp/v2/... requests. Add a per-request static array keyed by URL, then a persistent layer via wp_cache_get/wp_cache_set or transients keyed by a hash of the URL, and confirm reuse by counting wp_postmeta queries (e.g. with Query Monitor) on a page with repeated images. Done looks like: repeated URL lookups hit the cache and postmeta query count drops for repeated content and REST hits.
Written by the indexing model from the issue text.
Description
Impact: 75 · Confidence: 90 · Complexity: 40
Description
Problem
When Optimole's Optml_Lazyload_Replacer processes content (including REST API endpoints like /wp-json/wp/v2/...), it repeatedly calls core WordPress attachment_url_to_postid() to resolve image URLs to attachment IDs. In WordPress core, attachment_url_to_postid() queries the wp_postmeta table directly without built-in persistent caching. On pages or API endpoints with multiple images, this results in dozen of expensive wp_postmeta DB queries, severely slowing down PHP request execution time (e.g., ~6.25 seconds spent on 28 postmeta queries in a single request).
Desired Behavior
Optimole should cache the mapping between image attachment URLs and attachment IDs using WordPress Object Cache / Transients, or optimize the replacer pipeline so attachment_url_to_postid() is only executed when strictly necessary and cached across requests.
Acceptance Criteria
Optml_Lazyload_Replacercaches URL-to-attachment ID resolutions in WP Object Cache / transient storage.- Duplicate image URL lookups within the same request lifecycle reuse in-memory cache.
- Database query count for
wp_postmetaduring lazyload replacement drops significantly for repeated content/API hits.
Customer Context
A premium user noticed via New Relic profiling that Optml_Lazyload_Replacer is executing repeated attachment_url_to_postid() calls during REST API requests, causing 28 wp_postmeta database queries consuming ~6.25s of a 7.98s transaction.
Root Cause Analysis
The customer is asking to prevent or cache repeated URL-to-attachment-ID lookups. Underlying cause: attachment_url_to_postid() is uncached in WordPress core, and Optml_Lazyload_Replacer executes it on every image URL encountered during content replacement. This is an architecture performance issue in how existing replacement functionality interacts with WP database lookups. Improving existing behavior by introducing transient/object caching or in-memory static caching around attachment_url_to_postid() within Optimole solves the root cause without adding configuration surface.
Alternative Suggestions
Wrap attachment_url_to_postid() calls in Optml_Lazyload_Replacer with an internal static array cache for single-request scope and WP Object Cache / Transients (keyed by URL hash) across requests to eliminate repetitive wp_postmeta queries.
Source: HelpScout #3524196151
Generated by feature-request-triage workflow (ID: feature-request-triage_6b0599ff05a6a1.02374250)
- 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
-
Lazyload SVG placeholder runs uncached attachment lookups with JSON-escaped URLs on REST responsesOpencustomer report
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Codeinwp/optimole-wp#1253 ·
Maintainers usually reply within 1 day
-
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
-
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