wordpressuploadpopoeditmo

Add .po and .mo format to WordPress


I needed to add some formats such as ico and svg in a part of the WordPress theme that I designed, which I did by placing the following code in the functions.php file:

function upload( $mime_types ) {
  $mime_types[ 'svg' ] = 'image/svg+xml';
  $mime_types[ 'ico' ] = 'image/vnd.microsoft.icon';
  return $mime_types;
}
add_filter( 'upload_mimes', 'upload', 1, 1 );

Now I want to introduce two formats .po and .mo to WordPress for support.

My only problem is that I don't know what these two formats are and no matter what I searched for, I didn't find anything.

Does anyone know the type of these two formats? Or can I use application/msword (.doc) for them?


Edit:

I found the following tutorial for the .po and .mo format, but I still couldn't find the format type:

Opening PO Files

Media type


Solution

  • The answer by Dyn to the question How to make a header for gettext .po files? has your answer for the .po file:

    just download an example .po file and have a look inside with a regular text editor. The header contains the following information :
    "Content-Type: text/plain; charset=UTF-8\n"

    The mime type for .po is: text/plain.

    As for the .mo file, this is a binary file. RFC 2046 (MIME) Part Two: Media Types says:

    The "application" media type is to be used for discrete data which do not fit in any of the other categories, and particularly for data to be processed by some type of application program.
    The "octet-stream" subtype is used to indicate that a body contains arbitrary binary data.

    The mime type that can be used for .mo is: application/octet-stream.

    UPDATE

    It looks like there are at least two approaches you can try:

    1. wp_check_filetype_and_ext, and
    2. ALLOW_UNFILTERED_UPLOADS in wp-config.php

    I've tested both. They both work on WordPress version 6.4.

    wp_check_filetype_and_ext

    Nathan Johnson's answer from this question: Allow CSV files to be uploaded uses the WordPress filter hook wp_check_filetype_and_ext. The WordPress Docs for this hook say that it:

    Filters the "real" file type of the given file.

    wp_check_filetype_and_ext in addition to either the mime_types filter hook or the upload_mimes filter hook allow users with at least the Author role to upload .po and .mo files. You can follow Nathan Johnson's answer more closely to restrict this upload privilege to specific user roles. See code sample below.

    Code sample

    function add_my_mimes( $mime_types ) {
        $mime_types['mo'] = "application/octet-stream";
        $mime_types['po'] = "text/plain";
    
        return $mime_types;
    }
    add_filter( 'upload_mimes', 'add_my_mimes' );
    
    
    // Add filter to check filetype and extension
    function wpse_check_filetype_and_ext( $args, $file, $filename, $mimes ) {
        $extension = pathinfo( $filename )[ 'extension' ];
        if ( 'mo' === $extension ) {
            $args = array(
                'ext'             => 'mo',
                'type'            => 'application/octet-stream',
                'proper_filename' => $filename,
            );
        } elseif ( 'po' === $extension ) {
            $args = array(
                'ext'             => 'po',
                'type'            => 'text/plain',
                'proper_filename' => $filename,
            );
        }
        return $args;
    }
    add_filter( 'wp_check_filetype_and_ext', 'wpse_check_filetype_and_ext', 10, 4 );
    

    ALLOW_UNFILTERED_UPLOADS

    The wp-config.php update suggested under the heading Non-Image File Upload Error in this WP Engine post MIME Types in WordPress says:

    Add the following line to the wp-config.php file. This will allow non-image uploads for administrators only:

    define( 'ALLOW_UNFILTERED_UPLOADS', true );