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.

Hey I'm trying to make all .doc/docx .xls/xlsx files open in office online.

This would mean that every link to a .doc

http://mywebsite.com/wp-content/uploads.link.doc

would have to instead point to the office online preview link

https://view.officeapps.live.com/op/view.aspx?src=http://mywebsite.com/wp-content/uploads/link.doc

I'm not even sure how to go about doing this? Any help would be greatly appreciated!

Thanks!

share|improve this question

2 Answers 2

up vote 4 down vote accepted

I'm not sure this question is WordPress-related. It sounds like it might be handled via .htaccess redirect, or a browser extension.

That said: you could try to use the wp_get_attachment_url filter, that is applied to the URL returned by wp_get_attachment_url().

For example:

function wpse95271_filter_wp_get_attachment_url( $url ) {
    if ( 0 === stripos( strrev( $url ), 'cod.' ) ) {
        // This is a Word doc; modify the URL
        $url = 'https://view.officeapps.live.com/op/view.aspx?src=' . $url;
    }
    return $url;
}
add_filter( 'wp_get_attachment_url', 'wpse95271_filter_wp_get_attachment_url' );

(This is completely untested, and presented as an example only.)

Edit

a) how can I add .docx, .xls, .xlsx?

Just extend the conditional. Here's an example, abstracted for clarity:

function wpse95271_filter_wp_get_attachment_url( $url ) {
    // Using a simple ternary expression;
    // there may be better ways, such as
    // an array of doc extensions, and a
    // foreach loop, etc
    $is_msoffice_doc = (
        0 === stripos( strrev( $url ), 'cod.' ) ||
        0 === stripos( strrev( $url ), 'xcod.' ) ||
        0 === stripos( strrev( $url ), 'slx.' ) ||
        0 === stripos( strrev( $url ), 'xslx.' ) ? true : false
    );
    if ( $is_msoffice_doc ) {
        // This is an Office doc; modify the URL
        $url = 'https://view.officeapps.live.com/op/view.aspx?src=' . $url;
    }
    return $url;
}
add_filter( 'wp_get_attachment_url', 'wpse95271_filter_wp_get_attachment_url' );

b) instead of replacing the url can I add a link next to the normal download link that says "open online" with a link to this?

Just build your own link in your template, appending the attachment URL to your base URL, as done in the above filter.

share|improve this answer
    
Thanks that's really great, I guess we just need to find the conditional to identify them as .doc files! –  ameeromar Apr 10 '13 at 11:47
    
@ameeromar See my edit, test it. –  toscho Apr 10 '13 at 12:01
    
That is absolutely amazing!! Thank you so much, amazing code! two small questions: a) how can I add .docx, .xls, .xlsx? b) instead of replacing the url can I add a link next to the normal download link that says "open online" with a link to this? –  ameeromar Apr 10 '13 at 12:08
    
Re: the first question: just use the same conditional for any other document extensions you need; re: the second question: that's actually a separate question entirely. –  Chip Bennett Apr 10 '13 at 12:13
    
See edited answer, that attempts to answer your two questions. –  Chip Bennett Apr 10 '13 at 12:20

If you have inserted your MS Office document-links via the insert link button in the editor, you might try this one:

function edit_ms_office_links($content) {
    $viewer = "https://view.officeapps.live.com/op/view.aspx?src=";
    preg_match_all('/href="([^\"]*(doc|xls|docx|xlsx))"/i',$content,$match);
    if(isset($match[1])){
        foreach($match[1] as $link){
             $content=str_replace($link,$viewer.$link,$content);
        }
    }
    return $content;
}
add_filter('the_content','edit_ms_office_links');

this will add the $viewer part before the doc, xls, docx, xlsx documents links.

share|improve this answer
    
Thanks for that, unfotunately they are all attachments, not links! –  ameeromar Apr 10 '13 at 12:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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