WordPress Enqueue Styles/Scripts With Loop
I hadn't seen a snippet like this anywhere else so I figured I would share it since it sure saved me some time and I can see it helping others.
$js_dir = get_template_directory_uri() . "/js/";
$js_files = array("bootstrap", "retina", "main", "jquery.fancybox", "jquery.flexslider", "jquery.bxslider", "jquery.selectbox-0.2", "jquery.mousewheel", "jquery.easing");
foreach($js_files as $file){
$label = str_replace(".", "", $file);
wp_register_script($label, $js_dir . $file . '.js', array('jquery'), '1.0.0');
wp_enqueue_script($label);
}
Basically you put your files you wish to enqueue in the $js_files array. The values should be the filename without the .js at the end. This will work if the scripts you wish to enqueue are in the same directory, or else you will need to add some customizations.
Here is the same thing with styles:
$css_dir = get_template_directory_uri() . "/css/";
$css_files = array("bootstrap.min", "font-awesome.min", "flexslider", "jquery.bxslider", "jquery.fancybox", "jquery.selectbox", "media-query", "style", "mobile", "form-style", "settings", "wp");
foreach($css_files as $file){
$label = str_replace(".", "", $file);
wp_register_style($label, $css_dir . $file . '.css', array(), '1.0', 'all');
wp_enqueue_style($label);
}
Written by Dan Hunter
Related protips
1 Response
This is good, and it is simple create few variables and arrays with existing WordPress Function and reducing the lines of codes and time. Great Idea . Love it
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#