DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

When developing a plugin or theme, it can be useful to be able to programmatically create a directory within the wp-content/uploads folder. Here is a handy piece of code to do it.

Simply paste this code snippet on your functions.php file (or plugin file if you’re creating a plugin)

function myplugin_activate() {
    
    $upload = wp_upload_dir();
    $upload_dir = $upload['basedir'];
    $upload_dir = $upload_dir . '/mypluginfiles';
    if (! is_dir($upload_dir)) {
       mkdir( $upload_dir, 0700 );
    }
}
 
register_activation_hook( __FILE__, 'myplugin_activate' );

Thanks to Jean Galea for the snippet!

Tags

One Comment

  1. you can also use the wp_mkdir_p function

Leave a Comment

Your email address will not be published. Required fields are marked *