0

I've tried a few plugins to add mime types including the PJW Mime Config plugin. And adding this to a themes functions.php file would work fine, too, for a non-WPMS site. But I need these to be allowable throughout the network.

So I've written my own plugin to do the trick. After looking at info from the codex as well several blog posts and this WPSE question. This is what I've come up with:

function new_mime_types($mimes) {
    $mimes = array_merge($mimes, array( 
        'pdf' => 'application/pdf',
        'zip' => 'multipart/x-zip'
        //add your ext => mime to the array
        //there are a LOT more, but to save space I've left the rest out :)
        ));
    return $mimes;
}

// Hook
add_filter('upload_mimes', 'new_mime_types');

Now when I go to upload one of these file types, I receive a warning:

Warning: Cannot modify header information - headers already sent by (output started at [server]\wp-content\plugins\tps-mime-types.php:1) in [server]\wp-includes\pluggable.php on line 934.

I'm still new to WP, so any suggestions on what to do next?

Note: Also, I was going to tag this as upload-mimes and mime-types, but don't have the rep. :)


*// Update*

I've updated my plugin to the code below:

function new_mimes($mimes) {

    // add your mime to the txt file below
    // same formating and no spaces

    // mime-types.txt file set up like this...
    // pdf|application/pdf
    // zip|multipart/x-zip
    $file = "/tps-mime-types/mime-types.txt";
    $contents = file_get_contents( plugins_url( $file ) );
    $contents = str_replace( "\r", "|", $contents );
    $contents = str_replace( "\n", "|", $contents );
    $mime_types = explode( "|", $contents );

    $counter=0;
    foreach ( $mime_types as $ext_app ) {
        if( !isset( $ext_app[0] ) ) $ext_app[0] = $ext_app[1];
        $counter++;
    }

    // return the new full result
    return $mimes;
}

add_filter('upload_mimes', 'new_mimes');

But after activating the plugin on a WPMS install I've had for a while as well as a single install, I'm still experiencing problems. Upon activation:

The plugin generated 3 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

Then when I attempt uploading a file matching one of the new mime-types I receive my original error message:

Warning: Cannot modify header information - headers already sent by (output started at [server]\wp-content\plugins\tps-mime-types\tps-mime-types.php:1) in [server]\wp-includes\pluggable.php on line 934

Any ideas?

2

2 Answers 2

2

Obviously your plugin is producing an error!

More specifically, those allowed mime types are called in such a way that this error happens before WordPress actually starts sending output. The result: an error message get's printed, then WP tries to send its headers and your error happens.

Try this:

<?php
function wpse35410_new_mime_types($mimes) {
    if( ! isset( $mimes['pdf'] ) ) $mimes['pdf'] = 'application/pdf';
    if( ! isset( $mimes['zip'] ) ) $mimes['zip'] = 'multipart/x-zip';
    // etc.
    return $mimes;
}

I could be totally off here, but I suspect that's it. I was not able to reproduce the error with your code, so I'm not 100% sure.

3
  • Sarcasm aside, thanks for your answer ;P I'll give this a shot when I get back to work on Monday. Also, is there a way I can modify the mime types before the headers are sent? Wouldn't that fix my error?
    – joshmax
    Commented Dec 4, 2011 at 5:26
  • The upload_mimes filter is the only way to modify them. As long as your hooked function doesn't throw any PHP errors/warning it should be okay. Commented Dec 4, 2011 at 17:18
  • Thanks. You helped me get some new code working on a newer WP install, but another WPMS install is still not working. Please see updated info on question. THANKS!
    – joshmax
    Commented Dec 5, 2011 at 22:23
0

Figured it out! :D

Here's the new code:

// Add the filter
add_filter('upload_mimes', 'tqps_extended_mime_types');

// Function to add mime types
function tqps_extended_mime_types ( $mime_types=array() ) {

    // add your extension & app info to mime-types.txt in this format
    //   doc,doct application/msword
    //   pdf application/pdf
    // etc...
    $file = '/extended-mime-types/mime-types.txt';
    $file = plugins_url() . $file;
    $mime_file_lines = file($file);

    foreach ($mime_file_lines as $line) {
        //Catch all sorts of line endings - CR/CRLF/LF
        $mime_type = explode(" ",rtrim(rtrim($line,"\n"),"\r"));
        $mime_types[$mime_type[0]] = $mime_type[1];
    }

    // add as many as you like
    return $mime_types;
}

Be sure to save code as extended-mime-types.php inside a folder called extended-mime-types/. And create a text file called mime-types.txt.

mime-types.txt must be formatted as follows:

doc,doct application/msword
pdf application/pdf

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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