0

I'm writing a plugin that, as part of its functionality, uploads custom fonts. I'm using the new-since-Wordpress-3.5 media uploader & API.

I've read dozens of tutorials on how to change the upload path but am having problems making it work. Currently, it places the font files in the correct directory within the plugin, but throws a Wordpress error during the upload ('An error occurred in the upload. Please try again later.'), and, initially, doesn't show the uploaded file in the library. If you reload the plugin page & reopen the uploader, the file then shows up in the library, but the URL field is incorrect.

Upload error message

I've defined my plugin path & URL as constants in the main file:

    if ( !defined( 'MYPLUGIN_DIR' ) ) {
    define('MYPLUGIN_DIR', plugin_dir_path(__FILE__));
  };

  if ( !defined( 'MYPLUGIN_URL' ) ) {
    define('MYPLUGIN_URL', p`lugin_dir_url(__FILE__));
  };

Then, in the options file, I have:

  add_filter('wp_handle_upload_prefilter', 'myplugin_upload_prefilter');
  add_filter('wp_handle_upload', 'myplugin_handle_upload');

  // filter the upload_dir hook
  function myplugin_upload_prefilter( $file ) {

    add_filter('upload_dir', 'myplugin_upload_dir');
    return $file;

  }

  // unset the filter & return upload path to default
  function myplugin_handle_upload( $fileinfo ) {

    remove_filter('upload_dir', 'myplugin_upload_dir');
    return $fileinfo;

  }

  function myplugin_upload_dir($path) {

    $fontdir = 'assets/fonts'; // desired directory relative to plugin root  

    $path['path'] = MYPLUGIN_DIR . $fontdir;
    $path['url'] = MYPLUGIN_URL . $fontdir;
    $path['subdir'] = $fontdir;
    $path['basedir'] = MYPLUGIN_DIR;
    $path['baseurl'] = MYPLUGIN_URL;

    return $path; // altered

  }

An example of a malformed URL in the library router view is http://127.0.0.1/webroot/wp-content/uploads//Applications/MAMPSERVER-OR-WHATEVER/www/webroot/wp-content/plugins/myplugin/assets/fonts/myfont.woff. The URL should be http://127.0.0.1/webroot/wp-content/plugins/myplugin/assets/fonts/myfont.woff. Note the double slash after 'uploads'; I tried untrailingslashit to at least remove that, but it didn't work.

  1. Why does this code initially throw an error?
  2. Why does the library item not appear initially, but then appear on page reload & reopening the modal?
  3. How to fix wrong URL?

A separate, but related issue, is ensuring the path is altered only when media is uploaded from the plugin settings page, but I will post a separate question on that.

0

While I can't easily spot right now what is the cause of the bug, it is obvious that your approach is just wrong on two fronts

  1. The uploads from media library have no context, or at least it is not trivial to find out a context. An upload of font file by itself do not mean that it is related in any way to your plugin, and even if you were detecting file types before changing the upload directories, it would still have a potential of ending in a mess.

  2. It is a big no-no to try to upload to anywhere except for the uploads directory. In a properly managed server, the web server is unlikely to have permissions to write to any other directory.

What you should do is either find out how to give a context to the upload (probably very hard or just impossible), or better, move the upload to a new directory only when you know it is associated with your settings. Assuming the file is uploaded and you use the attachment id in your form, move the file when the form is being submitted.

I also suggest to look at how EDD does it, and get inspiration.

  • EDD? As to not uploading outside the uploads folder, I'm getting similar feedback elsewhere. So I will take that advice, for sure. – Chris Jun 6 '17 at 4:16
  • yes, EDD creates a directory under uploads and move the "download" files into it, never investigated exactly how, but I assume in the same way I described here – Mark Kaplun Jun 6 '17 at 4:40
  • Google says you might mean Easy Digital Downloads? – Chris Jun 7 '17 at 14:35
  • sorry, yes I do – Mark Kaplun Jun 7 '17 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.