Title: Programmatic way to enqueue scripts/styles
Last modified: November 2, 2024

---

# Programmatic way to enqueue scripts/styles

 *  Resolved [chris](https://wordpress.org/support/users/achensee/)
 * (@achensee)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/programmatic-way-to-enqueue-scripts-styles/)
 * Hi, this is not about a bug or an issue, but more a “feature” enhancement additional
   to the “Load Swiper JS on every page”: I would like to enqueue the public script
   and styles in a programmatic way on certain pages or custom templates. I know
   I can do it by adding wp_enqueue_script / style pointing to the files in the /
   plugin/ directory. But i wonder if there is already a method / function that 
   is available for that?
    -  This topic was modified 1 year, 6 months ago by [chris](https://wordpress.org/support/users/achensee/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [chris](https://wordpress.org/support/users/achensee/)
 * (@achensee)
 * [1 year, 3 months ago](https://wordpress.org/support/topic/programmatic-way-to-enqueue-scripts-styles/#post-18301156)
 * I still want to know how I can enqueue the wp swiper frontend script and styles
   for certain template pages. I parsed the code and tried the following method,
   but this doesn’t work.
 * `$wp_swiper_public = new WP_Swiper_Public('wp-swiper');
   add_action('wp_enqueue_scripts',
   function() use ($wp_swiper_public) {$wp_swiper_public->enqueue_frontend_assets();});
 * Also i tried just simply
 * `wp_enqueue_script('wpswiper-bundle-js');`
 * `wp_enqueue_style('wpswiper-bundle-css');`
 *  without success.
    -  This reply was modified 1 year, 3 months ago by [chris](https://wordpress.org/support/users/achensee/).
    -  This reply was modified 1 year, 3 months ago by [chris](https://wordpress.org/support/users/achensee/).
    -  This reply was modified 1 year, 3 months ago by [chris](https://wordpress.org/support/users/achensee/).
    -  This reply was modified 1 year, 3 months ago by [chris](https://wordpress.org/support/users/achensee/).
 *  Thread Starter [chris](https://wordpress.org/support/users/achensee/)
 * (@achensee)
 * [1 year, 3 months ago](https://wordpress.org/support/topic/programmatic-way-to-enqueue-scripts-styles/#post-18310984)
 * Solved and provided without any guarantee. For anyone who disable the option ‘
   Always load Swiper JS bundle on every page.’ and need to enqueue the wp-swiper
   Front-End Scripts on custom templates:
 *     ```wp-block-code
       // Swiper-JS Add Frontend Scripts and Stylesif (!function_exists('hf_load_swiper')) {    function hf_load_swiper() {        // Check if the class 'WP_Swiper_Public' exists        if (class_exists('WP_Swiper_Public')) {            $wp_swiper_public = new WP_Swiper_Public('wp-swiper', '1.0.0');            add_action('wp_enqueue_scripts', function () use ($wp_swiper_public) {                if ($wp_swiper_public instanceof WP_Swiper_Public) {                    if ($wp_swiper_public instanceof WP_Swiper_Public) {                        update_option('wp_swiper_options', ['enqueue_swiper' => 'on']); // set load swiper temporarily to on                        $wp_swiper_public->enqueue_frontend_assets(true); // Enqueue Swiper Front End Assets                        update_option('wp_swiper_options', ['enqueue_swiper' => '']); // reset load swiper                    }                }            });        }          }}
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Programmatic way to enqueue scripts/styles’ is closed to new replies.

 * ![](https://ps.w.org/wp-swiper/assets/icon-256x256.png?rev=2402464)
 * [WP Swiper](https://wordpress.org/plugins/wp-swiper/)
 * [Support Threads](https://wordpress.org/support/plugin/wp-swiper/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-swiper/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-swiper/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-swiper/reviews/)

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [enqueue](https://wordpress.org/support/topic-tag/enqueue/)

 * 2 replies
 * 1 participant
 * Last reply from: [chris](https://wordpress.org/support/users/achensee/)
 * Last activity: [1 year, 3 months ago](https://wordpress.org/support/topic/programmatic-way-to-enqueue-scripts-styles/#post-18310984)
 * Status: resolved