Adding eBook epub and mobi downloads to WordPress

A few technical notes if you want to add ebooks (.epub and .mobi file formats) downloads to your wordpress site. We encountered three obstacles, file size, upload restrictions and file association. Here are the solutions.

WordPress File Size Limits are Too Low

WordPress by default only allows 2 meg uploads and only of particular file types. Unfortunately .epub and .mobi are not among the default file types that are allowed by WordPress.

First the bad news – if you want to increase the allowed file sizes for uploads you have to modify the php.ini file. Depending on your hosting provider this may or may not be easy. See Bill Erickson’s post on increasing allowed file uploads in WordPress.

File Types That can be uploaded in WordPress

Hopefully this will change, but currently .mobi and .epub are obscure. So you have to configure WordPress to allow them on most hosting providers.

We are running the WordPress Thesis theme on this particular site so the solution to add file types for upload was found here and is abbreviated below.

In the Thesis custom_functions.php file add this php code using the WordPress editor. But don’t make a typo as after that you would have to FTP in to fix it. Hypothetically speaking. Here is the function modified from the link above.

function addUploadMimes($mimes) {
    $mimes = array_merge($mimes, array(
        'epub|mobi' => 'application/octet-stream'
    return $mimes;
add_filter('upload_mimes', 'addUploadMimes');

The above example only includes the .epub and .mobi examples, but if you want more just pipe-filetype further where the code says ‘epub|mobi’.

Change the Content-Type for epub Files Served by Apache and WordPress

All fixed, right? Not so fast. WordPress serves unknown files with the content-type of “Content-Type text/plain” in the HTTP header. You can see this using the  excellent long-lasting Rex Swain HTTP viewer.

The problem is that while Windows handles the misidentified .epub files fine, Macs freak out and show you the binary. You click it on a Mac and see garbage. Not cool. Must fix.

I found this post in the WebFaction support on adding epub to the .htaccess file to change content-type by file extension. A bit of modification and I found adding this to the application root .htaccess file did the trick

AddType application/epub+zip .epub
AddType application/x-mobipocket-ebook .mobi

A word of caution – BE CAREFUL WITH YOUR HTACCESS file! It probably already exists so be sure to download-backup-modify-upload-via-ssh for maximum security. Or really sFTP is plenty secure as well.

Other epub and mobi pit-falls to look out for

The ePub must be strictly validated. it is really a Zip compressed directory with it’s own MIME type specification. And XML that must be valid. Google “epub validation” if the above list did not solve your problem and go from there.