Cleaner ‘for each’ loop for functions.php

I’m looking for a elegant way to register a ton of scripts I have sitting in a js directory on my server without having to sit and type them all out.

I found this question here but it is 3 years old, I’m basically
after a similar solution and I would like to know if the following
code I have adapted from the original question is suitable for the

Or alternatively, if there a better solution at all?

add_action( ‘wp_loaded’, ‘theme_register_scripts’ );
function theme_register_scripts() {

$path = ‘/assets/js’;
$scripts = array_diff(scandir($path), array(‘.’, ‘..’));

foreach ( $scripts as $k=>$v )
wp_register_script( $k, get_template_directory_uri() . $v );

Thanks a bunch!

Read more here:: Cleaner ‘for each’ loop for functions.php

Leave a Reply

Your email address will not be published. Required fields are marked *