WordPress File Upload plugin for WordPress makes use of filters and actions for allowing the administrator to perform some advanced pre and post processing of the uploaded files.

There are currently two filters and one action:

  • Filter wfu_before_file_check: It is executed before file is uploaded and before any internal file checks, in order to allow the filter to perform its own checks or change some basic upload parameters, such as filename or userdata.
  • Filter wfu_before_file_upload: It is executed right before file is uploaded, in order to allow the filter to change the file name.
  • Action wfu_after_file_upload: It is executed after the upload process for each individual file has finished, in order to allow additional actions to be executed.

Suppose that we want to modify the filename of the uploaded file and add a timestamp at the end of it for every uploaded file. To do this, we will need to implement wfu_before_file_upload filter and write some php code.

For the moment the plugin does not provide a tool to write php code. We need to add a 3rd party plugin to allows us to do so. I personally like Custom css-js-php by Flippercode because it allows to easily write code for filters and actions. So, the instructions to implement the filter are the following:

  1. Install Custom css-js-php plugin by Flippercode and activate it.
  2. Go to CSS-JS-PHP section of your Dashboard and press “Add New PHP”.
  3. Give any title, like “Uploaded Filename Timestamping”
  4. Select Apply Using WP Filter.
  5. In the Filter Name write wfu_before_file_upload.
  6. Write the following code in the textbox below “Write your code here” and press Save.
function wfu_before_file_upload_handler($file_path, $file_unique_id) {
    // separate file name from file path
    $only_filename = basename($file_path);
    $only_filepath = substr($file_path, 0, - strlen($only_filename));
    // separate file extension from file name
    $dot_pos = strrpos($only_filename, ".");
    if ( $dot_pos ) {
        $ext_part = substr($only_filename, $dot_pos);
        $name_part = substr($only_filename, 0, $dot_pos);
    }
    // add timestamp to the end of name_part
    $current_datetime = gmdate("U");
    $only_filename = $name_part . "-" . gmdate("YmdHis", $current_datetime) . $ext_part;
    // return the full file path
    $file_path = $only_filepath . $only_filename;
    return $file_path;
}

Here is a screenshot:

screenshot4

After you do all the above and upload a file, the filename will get a timestamp. Within the above function you can do any other modification to the filename, however please note that the plugin will not check if the filename is valid (it has done so before the filter), so take care to modify the filename correctly.

Future versions of WordPress File Upload plugin will allow the administrator to add php code easily without the need of a 3rd party plugin.

Happy coding…

The Iptanus Team

How to Use Filters and Actions of WordPress File Upload Plugin

One thought on “How to Use Filters and Actions of WordPress File Upload Plugin

  • Hi Nickolas!

    I think this plugin is really good! I really appreciate all your hard work, and I like the way it can be integrated with WP-Filebase (which I also just found.)

    If I can get a question answered, then I think it will work for my needs and I will most certainly be buying the Pro version. It’s too cheap NOT to buy!! 🙂

    I put the shortcode inside of a bbPress forum reply page. It worked really great (aside from me needing to style it). The file was uploaded, the proper folder was created, and I can see it via FTP in the right place.

    However, I don’t know how to get the attachment to show on the forum post.

    Could you direct me to a shortcode or a hook or filter to use so that the attachment I created to go with the reply will show up on that reply?

    In other words, if I upload and attach a file to a forum reply, I’d like that reply, when shown to others, to show that the has an attachment with it.

    Thanks so much! Cheers!
    ~Mizagorn

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.