0

I've been frankensteining this plugin (first time digging into plugins) using examples from Google, but I'm not well versed in PHP.

The goal is to generate the init script for swiper.js for each instance of the shortcode that calls for a slider and place all of them in one inline script. I can't tell if I'm heading in the right direction, and VS Code keeps on coming up with syntax errors with a lot of the examples that others say work. Halp!

Some issues I've read in leads me to believe I'm going to have a problem iterating each script with the way the $Var is setup in the example I used below. I also found the following which might also work? The problem for me is none of the use cases are really matching mine.

Passing a shortcode attribute to a sub-function

Right now my plan is to get the $uid variable into the function that composes the init script, dump that into the $init and send that into the send_to_footer function where it hopefully loops each one. Another concern I have is where the add_action( 'wp_footer', array ( __CLASS__, 'send_to_footer' ) ); is located.

I've spent the good part of 3 days researching and working on this and I have to switch gears for a couple days before I can revisit. Appreciate the assistance.

add_shortcode( 'ds-product-slider', array ( 'the_product_slider', 'load_shortcode' ) );

class the_product_slider {

    protected static $var = array();
    protected static $initbundle = array();

    public static function load_shortcode( $atts ) {
        $args = shortcode_atts( 
            array (
                'order' => 'date',
                'orderby' => 'date',
                'category' => array( 'shop' ),
                'featured' => 'false',
                'sid' => '',
            ),$atts );
        $order = $args['order'];
        $orderby = $args['orderby'];
        $posts = $args['posts'];
        $category = $args['category'];
        $featured = $args['featured'];
        $options = array(
        'order' => $order,
        'orderby' => $orderby,
        'posts_per_page' => $posts,
        'product_cat' => $category,
        'featured' => $featured
        );
        if (is_null($sid)) {
            $sid = uniqid(''); 
        } else {}
        
        ob_start();
        $products = wc_get_products( $options );
        global $post;
        ?>
        <!-- Swiper -->

        <div class="px-5 position-relative mb-4 product-slider">

            <div class="swiper-container" id="<?php print $sid; ?>">

                <div class="swiper-wrapper" >

                    <?php woocommerce_product_loop_start();
                            foreach ($products as $product) {
                                $post = get_post($product->get_id());
                                setup_postdata($post);
                                wc_get_template_part('content', 'product');
                            }

                    wp_reset_postdata();
                    woocommerce_product_loop_end()
                    ?>

                </div> <!-- .swiper-wrapper -->

                <!-- Add Pagination -->
                <div class="swiper-pagination"></div>

                    <!-- Add Arrows -->
            <div class="swiper-button-next"></div>
            <div class="swiper-button-prev"></div>

            </div><!-- swiper-container -->

        </div><!-- px-5 position-relative mb-5 -->

        <!-- Swiper End -->
        <?php
        // Get our output string and save to a variable
        $sliders = ob_get_clean();
        // Return the string appended to the content
        return $sliders;
        // Product Slider Shortcode End

        self::$var[] = self::compose_init($sid); 
        add_action( 'wp_footer', array ( __CLASS__, 'send_to_footer' ) );
        
    }

    public static function send_to_footer() {
        foreach (self::$initbundle as $theElementToFooter) {
             echo '
             <script type="text/javascript">
             ' . $theElementToFooter . '
             </script>
             ';
        }
    }

    public static function compose_init( $sid, $args = array() ) {
        // Compose Swiper Init
        ob_start();
        echo '
        var swiper' . $sid . ' = new Swiper(\'#' . $sid . '\', {
            breakpoints: {
                320: {
                    slidesPerView: 1
                },
                768: {
                    slidesPerView: 3
                },
                1199: {
                    slidesPerView: 3
                },
                1400: {
                    slidesPerView: 4
                }
            },
            spaceBetween: 30,
            pagination: {
                el: \'.swiper-pagination\',
                type: \'bullets\',
                clickable: true
            },
            navigation: {
                nextEl: \'.swiper-button-next\',
                prevEl: \'.swiper-button-prev\',
            },
        });
        '
        ;
        $init = ob_get_clean();
        self::$initbundle[] = self::send_to_footer($sid);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.