Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using W3 Total Cache's CDN connection to RackSpace CloudFiles to serve severall hundred media files. My goal is to get the files to download in the user's browser, rather than stream. According to the CloudFiles Documentation (http://docs.rackspacecloud.com/files/api/v1/cf-devguide-20111115.pdf) I can set the Content Disposition headers.

Does anyone know how this can be done via W3 Total Cache or, perhaps, around with it? Or, alternatively, do you know of another way to force downloads for files served via the CDN?

share|improve this question

closed as off-topic by Mark Kaplun, birgire, Pieter Goosen, G. M., ialocin Aug 25 at 8:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – Mark Kaplun, birgire, Pieter Goosen, G. M., ialocin
If this question can be reworded to fit the rules in the help center, please edit the question.

    
I was taking a look at the file w3-total-cache\lib\CF\cloudfiles.php and there are some functions over there that allow to pass the header. I think that is too technical to be resolved for free. It's doable but would take much more time then just a simple answer, and W3 Total Cache does a poor job on filters and actions to plug new functionality, so you would need to fork the plugin or create something from the scratch. –  Webord Feb 24 '12 at 3:34

1 Answer 1

up vote 6 down vote accepted
+200

This can be broken down to a simple task by adding a custom filter.

You will need to add the following to the _send_request method within the CF_Http class in cloudfiles_http.php line 1283.

$headers = $this->_make_headers($hdrs);
//Add this line below
$headers = apply_filters('cloudfiles_headers', $headers, $url_path, $method);

This will give you access to the headers from a plugin rather than making large edits to W3TC.

Within your plugin, you can create a function like this:

add_filter('cloudfiles_headers', 'wpse_42273_cloudfiles_headers', 0, 3);
function wpse_42273_cloudfiles_headers($headers, $url_path, $method){
    if($method != 'GET')
         return $headers;
    if(preg_match_all('~^.*/(.+\.(jpg|jpeg|png|gif|pdf|doc|docx|csv))$~i', $url_path, $matches)){
        $filename = $matches[1][0];
        $headers[] = "Content-Disposition: attachment; filename=$filename";
    }
    return $headers;
}

This will add the proper header to force the download if W3TC is fetching an object with a file extension listed in the regex above. This is a good way to control what files are served in the browser and what files have to be downloaded.

You can try this out in a plugin I created for your convenience. This may need some tweaking, but this is how I feel you should approach the issue.

Download the plugin here: http://3-3.me/K4SL

As a final note, this obviously edits the core of the W3TC plugin (though with just one line of code). This isn't a best practice, but it will work for you. This means any upgrade will override your changes. It might be prudent to rename the plugin (create a fork on your system) so WordPress doesn't try to update it.

Hope this helps you out!

share|improve this answer
    
Brian - This sounds like a winner! I'll test it out as quickly as possible and get back to you with the results. Thank you for putting this together! –  Jonathan Wold Feb 24 '12 at 15:34
    
@JonathanWold No problem! I don't have a cloud files account, so I couldn't test it as I would have liked. –  Brian Fegter Feb 24 '12 at 15:37
    
I just gave this a test this morning, in Firefox and in Safari - no success yet. I've made the line change to check the filter, installed the plugin as is. Here's an example resource: gycweb.org/media/sunday-morning-devotional-2 –  Jonathan Wold Feb 28 '12 at 12:31
    
This feels like it's really close.. Any ideas for troubleshooting? Firebug may be of help, I'm just still relatively new at it. Any ideas are welcomed, otherwise, I will keep experimenting and report back. –  Jonathan Wold Feb 28 '12 at 12:32
    
I looked into it further and noticed that MP3 was missing in the preg_match. Added it, no success yet. –  Jonathan Wold Feb 28 '12 at 12:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.