How to Allow .txt File Uploads in WordPress

How to Allow .txt File Uploads in WordPress full-article

Reading Time: 1 min, 59 sec

By default, WordPress has restrictions on the types of files you can upload through the media library. This is done to keep your site secure and prevent risky file uploads. However, sometimes you may want to upload additional file types, such as a simple .txt file. In this guide, I will explain what file types WordPress supports out of the box and how you can allow .txt file uploads safely.

What File Types Can You Upload in WordPress by Default?

WordPress supports a variety of common file formats. Here are the main categories:

Best WordPress Hosting

Bluehost is one of the best and secure WordPress hosting provider
  • Images: .jpg, .jpeg, .png, .gif, .ico, .webp
  • Documents: .pdf, .doc, .docx, .ppt, .pptx, .pps, .ppsx, .odt, .xls, .xlsx, .psd
  • Audio: .mp3, .m4a, .ogg, .wav
  • Video: .mp4, .m4v, .mov, .wmv, .avi, .mpg, .ogv, .3gp, .3g2

You will notice .txt files are not included in this list. If you try to upload one, WordPress will block it with the message:

“Sorry, this file type is not permitted for security reasons.”

How to Allow .txt File Uploads in WordPress

To allow .txt uploads, you need to tell WordPress that this file type is safe. You can do this by adding a small code snippet in your theme’s functions.php file or in a custom functionality plugin.

Here is the code:

function allow_txt_uploads($mimes) {
    $mimes['txt'] = 'text/plain';
    return $mimes;
}
add_filter('upload_mimes', 'allow_txt_uploads');

How This Works

  • The upload_mimes filter controls which file types are allowed.
  • By adding 'txt' => 'text/plain', we are letting WordPress know that .txt files are safe to upload.
  • After saving this code, you can go to Media > Add New and upload your .txt file without any errors.

A Word of Caution

While .txt files are generally safe, it is important to remember that allowing new file types always comes with some risk. Attackers could try to disguise malicious code in files. Although WordPress usually treats .txt files as plain text, you should only upload files from trusted sources.

Wrap Up

By default, WordPress does not allow .txt file uploads. But with a simple snippet using the upload_mimes filter, you can easily enable it. This gives you flexibility if you need to upload notes, instructions, or configuration files as part of your project. Just be mindful of security and only upload files you trust.

Deepanker profile image

Written by Deepanker

Deepanker Verma is an experienced WordPress developer who has been working on WordPress for more than 12 years. On TheWPGuides, he writes about WordPress, WordPress development, and WordPress plugins.

Leave a Reply

Your email address will not be published. Required fields are marked *

Need a Hand with Your WordPress Site?

Don't let technical issues slow you down. Our professional WordPress maintenance service handles updates, security, and performance optimization so you can focus on what you do best: creating content.

Learn More About Our Services