It would be a right way to enqueue the script using foreach loop?

It would be a right way to enqueue the scripts using foreach loop only for
jquery, jquery-ui-widget, jquery-UI-accordion, jquery-ui-slider, jquery-ui-tabs, jquery-ui-datepicker, Jquery-ui-dialog and Jquery-ui-button because I have to write it many times so

I have make it like this:

 $jquery_ui = array(
    'jquery',
    'jquery-ui-core',
    'jquery-ui-widget',
    'jquery-ui-accordion',
    'jquery-ui-slider',
    'jquery-ui-tabs',
    'jquery-ui-datepicker',
    'jquery-ui-dialog',
    'jquery-ui-button',
 );

 // Framework JS
foreach ($jquery_ui as $ui) {
    wp_enqueue_script($ui);
}

So I just want to know this laziness is a right way or not:)

2 Answers
2

Yes you can. But to make sure the script has not already been registered or enqueued, use wp_script_is() as follows:

foreach( $jquery_ui as $ui ) {
    if( !wp_script_is( $ui ) ) {
        wp_enqueue_script( $ui );
    }
}

This will prevent conflicts due to another instance of the script being already enqueued.

Leave a Comment