• Resolved HenrikNK

    (@henriknk)


    Hi there,

    I have been playing around with a sniplet enabling me to use an external link to a customizr slider – and all is well if I only want 1 of the slides to link externally.

    However I would like more slides to have extranal links, and unfortunatelly I’m a little lost when it comes to php modification, and this is why I’m now asking for help here.

    The sniplet I have used are as follows:

    function my_new_target() {
        return 'http://www.google.com';
    }
    
    add_filter('tc_slide_link_url' , 'my_slide_custom_link', 10, 2);
    function my_slide_custom_link( $slide_link , $image_id ) {
        //does nothing if the image's id is not the targeted one
        if ( MY-IMAGE-ID != $image_id )
            return $slide_link;
    
        add_action('wp_footer' , 'open_external_link_in_new_tab');
        function open_external_link_in_new_tab() {
            ?>
            <script type="text/javascript">
                jQuery(document).ready(function () {
                    ! function ($) {
                        var new_target = '<?php echo my_new_target() ?>';
                        //prevents js conflicts
                        "use strict";
                        console.log(new_target);
                        //checks if the target slide url exists first
                        if ( $('a[href="<?php echo my_new_target() ?>"]' , '#customizr-slider' ).length === 0 )
                            return;
    
                        //adds the new button html content after the original button
                        $('a[href="<?php echo my_new_target() ?>"]' , '#customizr-slider').attr('target' , '_blank');
                    }(window.jQuery)
                });
            </script>
            <?php
        }
        //sets a custom url for the targeted slide
        return my_new_target();
    }

    Any suggestions on how to modify this so I can add more ID’s and external links.

    I thank you in advance

Viewing 4 replies - 1 through 4 (of 4 total)
  • You can try another solution if you want, this is my idea, in your child-theme functions.php:

    // define an array of slide_id => link
    $slides_link = array(
        '18' => 'http://www.google.com',
        '16' => 'http://www.yahoo.com'
    );
    
    add_filter('tc_slide_link_url' , 'my_slide_custom_link', 10, 2);
    function my_slide_custom_link( $slide_link , $image_id ) {
        global $slides_link;
        if ( array_key_exists($image_id, $slides_link) )
            return $slides_link[$image_id];
        //does nothing if the image's id is not the targeted one
        return $slide_link;
    }
    add_filter('tc_slider_display', 'add_target_blank');
    function add_target_blank($html){
        global $slides_link;
        $patterns=array();
        foreach ( $slides_link as $slide_id => $link ){
            array_push($patterns, '|(a class="btn.*? href="'.$link.'")|');
        }
        // here we simply add the target blank to our custom links
        return preg_replace($patterns, '$1 target="_blank"', $html);
    }

    What do you think about this solution?

    Thread Starter HenrikNK

    (@henriknk)

    Once again you have solved my issue 🙂

    d4z_c0nf, your the man.

    Can’t thank you enough for your support.

    Glad you solved, you’re welcome HenrikNK!
    Could you mark this topic as resolved? Thanks 😉

    Thread Starter HenrikNK

    (@henriknk)

    Of course

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Sniplet modification’ is closed to new replies.