How to create a folder if it doesn’t already exist in WordPress?

Photo of author
Written By geekerhub

Lorem ipsum dolor sit amet consectetur pulvinar ligula augue quis venenatis. 

During the WordPress installation, we encountered an error because the uploads folder does not exist at wp-content/uploads.
Some hosting provider does create a folder while some don’t.

So to create a folder recursively we need to add a block of code snippets in a theme. The code will check whether the folder exists or not. If the folder doesn’t exist then the code snippet will create a folder recursively.

You need to add below code in your theme’s functions.php file indicating the folder path.

if (!file_exists('path/to/directory')) {
    mkdir('path/to/directory', 0777, true);

Note that 0777 is already the default mode for directories and may still be modified by the current umask.
The third argument is for recursive mode.

Leave a Comment