• Resolved chris

    (@achensee)


    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 4 months, 3 weeks ago by chris.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter chris

    (@achensee)

    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 month, 1 week ago by chris.
    • This reply was modified 1 month, 1 week ago by chris.
    • This reply was modified 1 month, 1 week ago by chris.
    • This reply was modified 1 month, 1 week ago by chris.
    Thread Starter chris

    (@achensee)

    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:

    // Swiper-JS Add Frontend Scripts and Styles
    if (!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)
  • You must be logged in to reply to this topic.