Is it possible to upload text files (e.g. JSON) to wordpress blog in any way?

I want to show some code but it needs to be in a raw format, is it possible to upload a simple text file? I’m aware of the file type restrictions but if there is a nifty trick to do this it would be great.

Related posts

1 comment

  1. function upload_file($file_url) {
        if ( ! filter_var($file_url, FILTER_VALIDATE_URL) ) return false;
        $get = wp_remote_get( $file_url );
        if ( ! is_wp_error(  $get ) ) {
          // check mime type if you want
          // $mime_type = wp_remote_retrieve_header( $get, 'content-type' );     
          return wp_upload_bits( basename($file_url), '', wp_remote_retrieve_body( $get ) );
        }
        return false;
    }
    
    // use it like
    upload_file('http://www.site.com/path/myfile.json');
    

    For what is returned see wp_upload_bits in codex.

Comments are closed.