Unreliable URL Replacement
-
Hello @masoudin! 5 Star reviewer here; absolutely love the software. Hope you’re well.
I’m experiencing a puzzling issue. Using the Classic Editor, the client has inserted images into the main content area using the default image insertion method in the classic WYSIWYG GUI. In some cases, the URL is successfully replaced by your filter, but in other cases it is not.
Are there any tips you can provide to help me debug this on my end?
Thank you, sir!
-
Thank you for reaching out to us!
Is it possible if you could share the image and/or the full HTML text of the Classic editor with me? I mean i need these to replicate the issue on my side so i can debug it!
Anything that can help me replicate the issue is appreciated!Hello @masoudin! Thank you for helping out!
Here is the frontend image html for a properly filtered image URL:
<img decoding="async" class="aligncenter wp-image-14792 perfmatters-lazy entered pmloaded" src="https://r2.polycarp.digital/john23-alamy-HPXC7G.jpg" alt="" width="292" height="422" data-src="https://r2.polycarp.digital/john23-alamy-HPXC7G.jpg" data-ll-status="loaded">Here is that same image html from the “code” tab in the classic editor:
<img class="aligncenter wp-image-14792" src="https://wp1.now.christendom.cloud/wp-content/uploads/john23-alamy-HPXC7G.jpg" alt="" width="292" height="422" />And here is the frontend image html for one that was not properly filtered:
<img decoding="async" class="aligncenter perfmatters-lazy entered pmloaded" src="https://wp1.now.christendom.cloud/wp-content/uploads/kamphaus.jpg" alt="kamphaus.jpg" width="200" height="260" data-src="https://wp1.now.christendom.cloud/wp-content/uploads/kamphaus.jpg" data-ll-status="loaded">Here is that same image html from the “code” tab in the classic editor:
<img class="aligncenter" src="https://wp1.now.christendom.cloud/wp-content/uploads/kamphaus.jpg" alt="kamphaus.jpg" width="200" height="260" />Note that I did already try disabling perfmatters’ image lazy-loading, and was able to confirm that the issue persists even with that perfmatters feature disabled.
Note also that PHP is 8.3; WP is 6.9; and the theme is Bricks Builder latest.
As I said, both of these
<img>tags were added by uploading directly into the classic main content area editor.These posts are many years old, and your plugin was added after the content was already published.
Lastly, the unsuccessful image URLs in question were all indeed successfully offloaded by your plugin to R2. I am using the same image in the “featured image” area of my template, and the URL is successfully replaced by your filter in this area on all pages/posts without issue. The issue only exists for images in the classic main content area.
Thank you again!
Hello again, @masoudin!
To the above, I will add that it was not lost on me the absence of the
wp-image-{ID}prefixed class on the unsuccessful unfiltered URL. To me, this indicates that the post author most likely copy-pasted an image, or an image URL, into the main content area, thereby making it inaccessible programatically.I am therefore marking this as “resolved”, as this is not an issue with your outstanding software; but instead is an example of what happens when content-creators make their own website, instead of hiring an actual web developer.
Any suggestions or tips you might have to fix this would be most appreciated!
Godspeed, sir.
Presumably, I could use something like
attachment_url_to_postid()as part of a single-run function that finds all<img>in the post content html that don’t have thewp-image-{ID}class, and then add that class? Presumably then your filter would work, because it hooks onto that class, correct? Or is there a more elegant way you recommend that doesn’t permanently alter data in the DB? @masoudin.Hello @masoudin. My apologies for the confusion, but it seems my above diagnosis is incorrect. I have identified images with the
wp-image-{ID}class prefix whose URLs are not being filtered. Here is an example:Frontend:
<img width="299" height="425" decoding="async" class="alignnone size-full wp-image-20822 perfmatters-lazy entered pmloaded" src="https://wp1.now.christendom.cloud/wp-content/uploads/st-alphonsus-liguori-public-domain.jpg" alt="" data-src="https://wp1.now.christendom.cloud/wp-content/uploads/st-alphonsus-liguori-public-domain.jpg" data-ll-status="loaded">“Code” tab in the classic editor:
<img class="alignnone size-full wp-image-20822" src="https://wp1.now.christendom.cloud/wp-content/uploads/st-alphonsus-liguori-public-domain.jpg" alt="" />Again, to be clear, I did already try disabling Perfmatters’ image lazy-loading, and was able to confirm that the issue persists even with that Perfmatters feature disabled.
Please let me know any other info you might need to help debug! I am grateful.
-
This reply was modified 4 months, 1 week ago by
James Joseph Finn. Reason: Added Perfmatters info
Hi @jamesjosephfinn
Thanks for the details!
One question, Are you using plugin like Jetpack or another plugin that integrates their CDN into WP?
Do you know this urlwp1.now.christendom.cloud?You’re welcome! Thank you @masoudin for the help!
Yes, that domain is my private server, not a CDN. We aren’t using Jetpack, nor any other CDN. On this site, the only CDN for images we are using is R2; and, when your filter works, the URL gets filtered to
r2.polycarp.digital, our custom domain on R2. When your filter doesn’t work, the original domainwp1.now.christendom.cloudremains unfiltered.@jamesjosephfinn
Could you please check one of these images that are loading fromwp1.now.christendom.cloudto see if they are already offlaoded or not? What is their offloading status?@masoudin, for the image above,
st-alphonsus-liguori-public-domain.jpg, that is indeed successfully offloaded to R2 by your software. In fact, in that same page template, I am referencing that same image (which is the “Featured Image”), in a different area outside of the “main content” area, and that particular image URL is successfully filtered / replaced by your software. It is only inside the “main content” area, where the post author has inserted that same image into their article, using the classic editor, where the existing filter fails to function.Hi @jamesjosephfinn ,
Thank you for the details!
The currentwp_content_img_tagfilter that we are using to rewrite the image URLs in the post contents, relies on WordPress being able to identify the attachment ID from thewp-image-{ID}class. This filter may not fire correctly when:- Page builders (like Bricks) have their own rendering engines
- Content is processed in non-standard ways
- Images lack the
wp-image-{ID}class
Workaround to Test:
Please add this function to your theme’s functions.php and check if it resolves the issue:
/**
* Fallback filter to rewrite all upload URLs to S3/R2 in post content.
*/
add_filter('the_content', 'advmo_rewrite_content_urls', 999);
add_filter('render_block', 'advmo_rewrite_content_urls', 999);
function advmo_rewrite_content_urls($content) {
if (empty($content)) {
return $content;
}
$settings = get_option('advmo_settings');
$provider = $settings['cloud_provider'] ?? '';
if (empty($provider)) {
return $content;
}
$provider_key = advmo_get_cloud_provider_key();
$domain = advmo_get_provider_credential($provider_key, 'domain');
if (empty($domain)) {
return $content;
}
$domain = advmo_normalize_url($domain);
$upload_dir = wp_get_upload_dir();
$upload_base_url = $upload_dir['baseurl'];
// Remove protocol from upload base URL to match both http and https
$upload_base_url_no_protocol = preg_replace('/^https?:\/\//i', '', $upload_base_url);
// Pattern matches both http:// and https:// versions of upload URLs
$pattern = '/https?:\/\/' . preg_quote($upload_base_url_no_protocol, '/') . '\/([^"\'>\s]+\.(jpe?g|png|gif|webp|avif))/i';
return preg_replace_callback($pattern, function($matches) use ($domain, $upload_base_url) {
$full_url = $matches[0];
$relative_path = $matches[1];
// Normalize URL to match what's stored in DB (use the WP upload base URL protocol)
$normalized_url = $upload_base_url . '/' . $relative_path;
$attachment_id = attachment_url_to_postid($normalized_url);
if (!$attachment_id) {
$attachment_id = attachment_url_to_postid($full_url);
}
if (!$attachment_id) {
// Try without size suffix (e.g., -300x200)
$base_path = preg_replace('/-\d+x\d+(\.[^.]+)$/', '$1', $relative_path);
$base_url = $upload_base_url . '/' . $base_path;
$attachment_id = attachment_url_to_postid($base_url);
}
if (!$attachment_id) {
return $full_url;
}
if (!get_post_meta($attachment_id, 'advmo_offloaded', true)) {
return $full_url;
}
$advmo_path = get_post_meta($attachment_id, 'advmo_path', true);
$filename = basename($relative_path);
return rtrim($domain, '/') . '/' . ltrim($advmo_path, '/') . $filename;
}, $content);
}I would appreciate if you could share the result with us. It will help us to address the issue in the plugin for the next releases.
Hello @masoudin,
I am happy to report that, yes, the patch functions as intended. The img URLs in the post content are replaced successfully.
For me, this issue is resolved! However, I won’t mark it as “Resolved” just in case you have your own issue tracking workflow around the “new releases” you mentioned.
I do have one question: Your
ifblock with the comment: “Try without size suffix (e.g., -300×200)”, it is my understanding thatattachment_url_to_postid()handles “thumbnail” suffixes automatically, but you probably have that in there for a reason, and I’m always trying to learn new things!Peace and goodness!
I am glad to hear the issue has been resolved for you. I will mark this as resolved on our end as well. We are tracking this in our internal system and a permanent fix will be included in the next release, at which point you can remove the snippet from your functions.php file.
Regarding
attachment_url_to_postid(), it is my understanding that it does not always work reliably with specific image sizes. I may be mistaken, but that has been my experience when working with it previously.Excellent! You are a gentleman and a scholar.
I’ve not ever used
attachment_url_to_postid(), so it’s good to know about your experience in this regard, for future reference!I remain immensely grateful for you and the outstanding software!
-
This reply was modified 4 months, 1 week ago by
You must be logged in to reply to this topic.