Home / Admin / create_folder_and_upload_file
Duplicate Snippet

Embed Snippet on Your Site


Creating a folder directly inside the WordPress Media Library through code requires understanding that the WordPress Media Library itself does not use physical folders to organize media. Instead, it uses database entries to manage and categorize media items. However, you can programmatically upload files to specific directories within the wp-content/uploads directory, which indirectly allows you to create a "folder" by specifying a path during the upload process.

Code Preview
function create_custom_directory($dir_name) {
    $upload_dir = wp_upload_dir(); // Get the uploads directory array
    $custom_dir = $upload_dir['basedir'] . '/' . $dir_name; // Specify the custom folder
    if (!file_exists($custom_dir)) {
        if(file_exists($custom_dir)) {
            return "Directory created successfully.";
        } else {
            return "Failed to create directory.";
    } else {
        return "Directory already exists.";
// Example usage
$dir_name = 'en';
echo create_custom_directory($dir_name);


Add a Comment