I just setup W3 Total Cache S3 CDN (origin push) and most of the URLs are being rewritten correctly but we have some custom plugins where the URLs are not being rewritten. How do I grab the rewritten URL of W3TC from another plugin? I’m currently using the following code to grab the image:
wp_get_attachment_image_src( get_post_thumbnail_id(), 'car-thumb');
I’m assuming there is a filter of some sort that just replaces the original “origin” url with the asset URL on the S3 bucket but I’ve been unable to find where in the code.
The W3 Total Cache plugin changes the URL of various files in /w3-total-cache/lib/W3/Plugin/Cdn.php in the function *ob_callback*. It uses a series of callbacks to modify an output buffer. The code runs like this:
$root->run();
$plugin->run()
for each plugin in$this->_loaded_plugins
$GLOBALS['_w3tc_ob_callbacks']
Which callbacks then run is unfortunately hard coded in this line:
$buffer = w3tc_do_ob_callbacks(array('minify', 'newrelic', 'cdn', 'browsercache', 'pagecache'), $buffer);
Because this is hard coded, if you ever need to modify what is included and what isn’t, you’ll have to change their callback.
Example:
I have a plugin that exports JSON, and the CDN aspect of W3 Total Cache wasn’t changing any URLs for JSON requests. It turns out that my output was failing the
w3_is_xml($buffer)
test.I fixed it by turning their single CDN callback into multiples, like this:
Then doing the changes I need, making sure to call their original callback in the middle.
I had a similar issue when rendering a template part.
I had to invoke the W3_Plugin_Cdn plugin programmatically.
As correctly stated by Tyler V., the
w3_is_xml($buffer)
will fail if $buffer doesn’t look like HTML or XML, that’s why I’m putting<html>
at the beginning of the template part.Here’s an example on how I did.
I had the same issues and couldn’t get an answer either. I did however find where in the database WP is storing the entries for the attached images and used that to create my own rewritten URLs.
If you look in the _postmeta table there is a meta_key “_wp_attached_file” that stores the folder path to the attached images. With this information I only needed to add the URL structure for where our S3 bucket is located.