Don’t Use PHP’s Mkdir in WordPress

PHP has some pretty great functions for handling the file system, mkdir, and rmdir particularly come to mind as, for the most part, they do the job quite well.

As a WordPress developer however that doesn’t mean they’re both your best choice.

WordPress actually has a function that will do this better (especially considering the unpredictability of file structures on many WordPress sites.

Enter wp_mkdir_p()

wp_mkdir_p( $target ) takes the guess work out of creating a directory by not only recursively trying to create the directory you specify in the $target but also by trying to make sure that the permissions are correct for WordPress (so no 0777 or unable to read/write errors later).

Of course, given the nature of WordPress and how well it handles things like media uploads you should probably be asking yourself just how necessary it is to create your own directory however, if there is no getting around it, at least try to minimize the chance for problems by using wp_mkdir_p instead of PHP’s own mkdir.

About Chris Wiegman

Chris is an engineer at WP Engine who has been working on WordPress since 2008. Over the years he built one of the largest security plugins on as well as numerous other plugins, themes and solutions for sites large and small. When not coding Chris loves to teach and has presented at numerous WordCamps and other conferences as well as taught computer security for St. Edward’s University and other University courses ranging from computers to aviation.

Find Chris on Facebook, GitHub,, and Twitter.

Comments are closed.