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

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