Register all css file in child theme subfolder

Hi @dutuka,

I’m not familiar with *.css. Do you have a document regarding this form of folder content CSS enqueueing? We’ll try to see how it goes.

Alternatively, a code that could work is something like this:

function enqueue_styles_from_folder() {
    $folder_path = get_template_directory() . '/css/'; // Replace 'css/' with your folder path

    if ($handle = opendir($folder_path)) {
        while (false !== ($file = readdir($handle))) {
            if ('.' !== $file && '..' !== $file && '.css' === substr($file, -4)) {
                $file_uri = get_template_directory_uri() . '/css/' . $file; // Replace 'css/' with your folder path
                wp_enqueue_style('style-' . $file, $file_uri);
            }
        }
        closedir($handle);
    }
}
add_action('wp_enqueue_scripts', 'enqueue_styles_from_folder');

It’s a loop that checks for .css files in a specific directory, and enqueues it.

This is more of a general WordPress concern though as opposed to being theme-related. It would be good to ask in the WP forums as well:

Thread Starter
dutuka

(@dutuka)

Hi ernandoazarcon2,

thank you for your help, I just tested the code, replace my custom folder path, unfortunately the code does not work.

I use this snippet to include all .php files in custom folder:

$filepath = dirname(FILE).'/custom-php/';
$files = scandir($filepath);
foreach ($files as $file) {
// match the file extension to .php
if (substr($file,-4,4) == '.php') {include($filepath.$file);}
}

May be you can combine this to enqueue all .css files in custom folder.

Best thanks in advanced.

Can you share the entire code?

Thread Starter
dutuka

(@dutuka)

It is the entire code snippet, i put it in the functions.php file, all the .php files in /custom-php/ folder in the child theme will be included.

$filepath = dirname(FILE).'/custom-php/';
$files = scandir($filepath);
foreach ($files as $file) {
// match the file extension to .php
if (substr($file,-4,4) == '.php') {include($filepath.$file);}
}

Now i want to achieve the same for all .css files in the /custom-css/ folder in the child theme.

  • This reply was modified 12 hours, 9 minutes ago by dutuka.

There should be something like this code for that code to work:

add_action('wp_enqueue_scripts', 'enqueue_styles_from_folder');

Can you check?

Thread Starter
dutuka

(@dutuka)

Im trying to test around but still not work.

Is there any enqueue code in your functions.php?

This isn’t GP related as well as mentioned. It would be best to ask here instead:

Thread Starter
dutuka

(@dutuka)

Thanks for the link, there is no other enqueue code in my functions.php, but i consider to use only one css file, its better for performance but not so comfort to maintain.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer