How to share ebooks with WordPress (EPUB and MOBI files)

If you’ve ever tried to upload an ebook in .MOBI or .EPUB format with the WordPress Media Uploader, you will have noticed an error message appear. Something along the lines of “Sorry, this file type is not permitted for security reasons”.

The only way then appears to be to ZIP the file and share it. That’s not a great experience for mobile users, who would simply like to click on a file and open it in an application such as iBooks or Kindle.

The solution to this puzzle lies in adding the required mime types to WordPress, so that these file types are allowed.

Let me show you how to do it in this article.

Ebook Types

There are three popular ebook file types in use today (as of 2018). Those are:

  • .MOBI format (used by Amazon’s Kindle devices)
  • .EPUB format (used by iBooks, Kobo, Nook and Sony)
  • .PDF format (used on all platforms)

MOBI and EPUB are more or less very long HTML web pages and use a similar markup for formatting, whereas the PDF format is akin to a series of images. Hence a PDF version is often larger than their counterparts.

Amazon’s Kindle uses the MobiPocket format (with a .MOBI extension), which is more or less the same as their proprietary .AZW, which in turn is based on the same format – just in case you ever come across it.

Direct PDF uploads are supported by default on WordPress, but .MOBI and .EPUB files are not.

Adding MIME types

To allow our two ebook file types to be uploaded, we need to add their respective mime types to an array that WordPress keeps track of. We do this by hooking into the upload_mimes filter using a function of our own.

This can be done for any file type, but the trick is that we need to know which MIME type is being used for which file extension. Thankfully, Mozilla keeps a huge list of pretty much every single MIME type in existence today. Thank you, Mozilla!

Consulting that list, we quickly find what we’re looking for. Here’s a sample function that adds both MIME types to the relevant array in WordPress. Add this code to your child theme’s functions.php file:

function allow_personal_uploads ( $existing_mimes=array() ) {

    // allow uploading .MOBI and .EPUB files
    $existing_mimes['mobi'] = 'application/x-mobipocket-ebook'; 
    $existing_mimes['epub'] = 'application/epub+zip'; 

    // return amended array
    return $existing_mimes;
}
add_filter('upload_mimes', 'allow_personal_uploads');

Without a restart or anything, your Media Uploader will now allow the additional file types.

Happy ebook sharing!





You can leave a comment on my original post.