How to create a subdirectory in WordPress’ “upload” directory upon plugin activation

As I was developing a plugin I needed it to create a directory named “biap_uploads” below /WP_ROOT/wp-content/uploads/ (i.e. /WP_ROOT/wp-content/uploads/biap_uploads) upon its activation. I thought no task better than to write my first how-to for WordPress Plugin development than here.

Step 1

To accomplish this, I created a function called biap_activate in my theme’s functions.php like so:

function biap_activate() {
  $upload_dir = wp_upload_dir();
  $upload_loc=$upload_dir['basedir']."/biap_uploads";
  if (!is_dir($upload_loc)) {
    wp_mkdir_p($upload_loc);
    }
  }

What this essentially does is set $upload_dir equal to the WordPress “wp_upload_dir();” which (and I quote) “[r]eturns an array of key => value pairs containing path information on the currently configured uploads directory.”

I then retrieve set the value of key ‘basedir’ from the newly created array $upload_dir to $upload_loc. The value associated with the key ‘basedir’ returns WordPress’ currently configured uploads directory, for me it was /PATH/TO/WORDPRESS/wp-content/uploads -> I appended to this variable forward-slash and my new directory name … so $upload_loc was set to “/PATH/TO/WORDPRESS/wp-content/uploads/biap_uploads” .
The next thing I did was check if that directory existed using PHP’s native is_dir function and if so, I used WordPress’ wp_mkdir_p function to create the directory.

Step 2

I then added to my theme’s functions.php a hook to this function using register_activation_hook like so:

register_activation_hook( __FILE__, 'biap_activate' );

Note how I used __FILE__, which is one of PHP’s  magic constants, more on that in a moment.

This function is a shortcut and is, per the WordPress codex is “more reliable method than using the activate_pluginname action. ” The first argument for register_activation_hook is path to the main plugin file, which so long as you are creating this function in the main plugin file __FILE__ will suffice. If not, then you will need to find a way to refer to the main file. The second argument is the function you want to run upon activation.

Conclusion

This works just fine on my local installation (a VPSLatch account running CentOS 5 32 Bit) , but when I release the plugin to beta testers I’m sure I will have to make some tweaks.

I will likely hook into the deactivation of the plugin a function that destroys all of the uploaded data when the plugin is deactivated but before I do that I want to look into the implications and find a way to give the users fair warning that media will be deleted and an easy way to back up the media before destroying it.

Until then…

2 Responses to “How to create a subdirectory in WordPress’ “upload” directory upon plugin activation”

  1. Steinar Knutsen

    Mat – thanks for sharing this!! It was a huge life saver for me tonight.

    I’m curling in files from a site feed and storing them in the Uploads folder.

    Took me forever to figure it all out and your piece was critical – so thanks again!!

    - Steinar

    Reply
  2. Mat

    Glad I could help :)

    Reply

Leave a Reply