I’m trying to add all my custom Js and CSS files located in my theme folder (themes/rev_cust/layout, and themes/rev_cust/s):

/* Add Scripts */

if ( ! function_exists('rev_cust_styles')) {
    add_action( 'wp_enqueue_scripts', 'rev_cust_styles' );
    function rev_cust_styles() {
        $css_paths = array("layout", "js");

        foreach ($css_paths as &$value) {

            $dir = get_stylesheet_directory() . '/' . $value;
            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST );

            foreach ( $iterator as $path ) {
                if ( ! $path -> isDir() ) {
                    $path_parts = pathinfo($path);
                    $file_parts = $path_parts['extension'];

                    $file_name = basename($path, '.css');
                    $pages_base = "/opt/lampp/htdocs/projects/wordpress_projects/liz_bloggable";
                    $server_pages_base = "http://localhost/projects/wordpress_projects/liz_bloggable";

                    $url = str_replace($pages_base, $server_pages_base, $path);

                    if ($file_parts == "css") {
                        // print('<div>' . $url . '</div>');
                        wp_enqueue_style( $file_name, $url);
                    } else if ($file_parts == "js") {
                        // print('<div>' . $url . '</div>');
                        wp_enqueue_scripts( $file_name, $url);
                    }
                }
            }
        }
    }
}

I, however, get the following error:

Fatal error: Uncaught UnexpectedValueException: RecursiveDirectoryIterator::__construct(/opt/lampp/htdocs/projects/wordpress_project/wp-content/themes/rev_cust/layout): failed to open dir: Too many open files in /opt/lampp/htdocs/projects/wordpress_project/wp-content/themes/rev_cust/functions.php:35 Stack trace: #0 /opt/lampp/htdocs/projects/wordpress_project/wp-content/themes/rev_cust/functions.php(35): RecursiveDirectoryIterator->__construct('/opt/lampp/htdo...') #1 /opt/lampp/htdocs/projects/wordpress_project/wp-includes/class-wp-hook.php(298): rev_cust_styles('') #2 /opt/lampp/htdocs/projects/wordpress_project/wp-includes/class-wp-hook.php(323): WP_Hook->apply_filters('', Array) #3 /opt/lampp/htdocs/projects/wordpress_project/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #4 /opt/lampp/htdocs/projects/wordpress_project/wp-includes/script-loader.php(1221): do_action('wp_enqueue_scri...') #5 /opt/lampp/htdocs in /opt/lampp/htdocs/projects/wordpress_project/wp-content/themes/rev_cust/functions.php on line 35

How can I fix this?

Thank you all in advance.

Read more here: How to recursively add script files (such as .css and .js) to a WordPress theme


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: