Title: Got some code for you
Last modified: August 21, 2016

---

# Got some code for you

 *  Resolved [william.staves](https://wordpress.org/support/users/williamstavespacecocom/)
 * (@williamstavespacecocom)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/got-some-code-for-you/)
 * I used your agnosia plugin – then wound up basically rewriting it.
 * Thought I would return the favor by sharing the revamped code with you.
 *     ```
       <?php
       /**
        * @package Bootstrap_Carousel
        * @version 0.3
        */
       /*
         Plugin Name: Bootstrap Carousel
         Plugin URI: http://wordpress.org/extend/plugins/agnosia-bootstrap-carousel/
         Description: A gallery replacment shortcode that generates HTML and javascript for a Bootstrap style Carousel
         Author: William Staves
         Author URI: http://pacecommunications.com/
         Version: 0.2
        */
   
       add_shortcode( 'bootstrap_carousel', 'pi_bootstrap_carousel_gallery_shortcode' ); /* Add custom shortcode */
       add_shortcode( 'pi_bootstrap_carousel', 'pi_bootstrap_carousel_gallery_shortcode' ); /* Add custom shortcode */
   
       function pi_bootstrap_carousel_gallery_shortcode( $attr )
       {
           static $instance = 0;
           $instance++;
   
           extract( shortcode_atts( array(
               'ids' => '',
               'name' => 'agnosia-bootstrap-carousel_'.$instance,
               'width' => '',
               'height' => '',
               'indicators' => 'before-inner',
               'control' => true,
               'interval' => 5000,
               'pause' => 'hover',
               'titletag' => 'h4',
               'title' => true,
               'text' => true,
               'wpautop' => true,
               'containerclass' => '',
               'itemclass' => '',
               'captionclass' => '',
                           ), $attr ) );
   
           if( $interval == '0' )
               $interval = false;
           $control = ( bool ) $control;
           $title = ( bool ) $title;
           $text = ( bool ) $text;
           $wpautop = ( bool ) $wpautop;
   
           $images = explode( ',', $ids );
   
           $unsorted_posts = get_posts( array(
               'post__in' => $images,
               'post_type' => 'attachment',
               'posts_per_page' => -1
                   ) );
   
           //assert image order
           $posts = array( );
           foreach ( $images as $image_id )
           {
               foreach ( $unsorted_posts as $i => $post )
               {
                   if( $post->ID == $image_id )
                   {
                       $posts[] = $post;
                       unset( $unsorted_posts[$i] );
                   }
               }
           }
   
           $output = '';
           if( is_array( $posts ) and !empty( $posts ) )
           {
               /* Define width of carousel container */
               $container_style = '';
               if( $width )
                   $container_style = 'style="width: '.$width.'"';
   
               $item_style = '';
               if( $height )
                   $item_style = 'style="width: '.$height.'"';
   
               //Loop once to build panels and indicators
               $indicators_html = '<ol class="carousel-indicators">';
               $panels = '';
               foreach ( $posts as $i => $post )
               {
                   $class = '';
                   if( $i == 0 )
                       $class = 'active ';
                   //build indicators
                   $indicators_html .= '<li data-target="#'.$name.'" data-slide-to="'.$i.'" class="'.$class.'">';
   
                   //build inner panels
                   $image = wp_get_attachment_image_src( $post->ID, 'full' );
                   $panels .= '<div style="z-index:90;" class="'.$class.'item '.$itemclass.'" data-slide-no="'.$i.'" '.$item_style.'>';
                   $panels .= '<img alt="'.$post->post_title.'" src="'.$image[0].'" />';
   
                   if( $title || ($text && !empty( $post->post_excerpt )) )
                   {
                       $panels .= '<div class="carousel-caption '.$captionclass.'">';
                       if( $title )
                           $panels .= '<'.$titletag.'>'.$post->post_title.'</'.$titletag.'>';
   
                       if( $text )
                       {
                           if( $wpautop )
                               $panels .= wpautop( $post->post_excerpt );
                           else
                               $panels .= $post->post_excerpt;
                       }
                       $panels .= '</div>';
                   }
                   $panels .= '</div>';
               }
               $indicators_html .= '';
   
               /* Initialize carousel HTML. */
               $output = '<div style="z-index:100;" id="'.$name.'" class="carousel slide '.$containerclass.'" '.$container_style.'>';
   
               /* Try to obtain indicators before inner. */
               if( $indicators == 'before-inner' )
                   $output.=$indicators_html;
   
               /* Initialize inner. */
               $output .= '<div class="carousel-inner">'.$panels.'</div>';
   
               /* Try to obtain indicators after inner. */
               if( $indicators == 'after-inner' )
                   $output.=$indicators_html;
   
               if( $control )
               {
                   $output .= '<a href="#'.$name.'">‹</a>';
                   $output .= '<a href="#'.$name.'">›</a>';
               }
   
               /* Try to obtain indicators after control. */
               if( $indicators == 'after-control' )
                   $output.=$indicators_html;
   
               /* End carousel HTML. */
               $output .= '</div>';
   
               /* Obtain javascript for carousel. */
               ob_start();
               ?>
               <script type="text/javascript">
                   jQuery(document).ready(function() {
                       jQuery('#<?php echo $name ?>').carousel({interval: <?php echo ($interval === false) ? 'false' : $interval ?>, pause: "<?php echo $pause ?>"});
                       jQuery('#<?php echo $name ?>').swipe({
                           //Generic swipe handler for all directions
                           swipe: function(event, direction, distance, duration, fingerCount) {
                               if (direction == 'left')
                                   jQuery('#<?php echo $name ?>').carousel('next')
                               else if (direction == 'right')
                                   jQuery('#<?php echo $name ?>').carousel('prev')
                           },
                           //Default is 75px, set to 0 for demo so any distance triggers swipe
                           threshold: 0
                       });
                   });
               </script>
               <?php
               $script = ob_get_contents();
               ob_end_clean();
               $output.=$script;
           }
           return $output;
       }
       ```
   
 * _[Moderator Note: Please post code or markup between backticks or use the code
   button. Or better still – use a [pastebin](http://pastebin.com/). Your posted
   code may now have been permanently damaged by the forum’s parser.]_
 * [http://wordpress.org/plugins/agnosia-bootstrap-carousel/](http://wordpress.org/plugins/agnosia-bootstrap-carousel/)

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

 *  Thread Starter [william.staves](https://wordpress.org/support/users/williamstavespacecocom/)
 * (@williamstavespacecocom)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/got-some-code-for-you/#post-4180513)
 * note:
    instead of hijacking the gallery hook, this is actually a new shortcode.
 * so instead of [gallery type="carousel" ids="1,2,3,4"]
    you’d use [bootstrap_carousel
   ids=”1,2,3,4″]
 * there’s still a simple substitution but this way the gallery hook is not tampered
   with. (Wouldn’t work if someone else tried to do the same thing the same way)
 *  Plugin Author [Andrés Villarreal](https://wordpress.org/support/users/andrezrv/)
 * (@andrezrv)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/got-some-code-for-you/#post-4180533)
 * Hi, Steven. Thank you very much for your feedback!
 * I’ll study your code and see if I can add some of it in the next version of the
   plugin.
 * Cheers, and thanks a lot again.

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

The topic ‘Got some code for you’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/agnosia-bootstrap-carousel_bfb2a9.
   svg)
 * [Agnosia Bootstrap Carousel by AuSoft](https://wordpress.org/plugins/agnosia-bootstrap-carousel/)
 * [Support Threads](https://wordpress.org/support/plugin/agnosia-bootstrap-carousel/)
 * [Active Topics](https://wordpress.org/support/plugin/agnosia-bootstrap-carousel/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/agnosia-bootstrap-carousel/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/agnosia-bootstrap-carousel/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Andrés Villarreal](https://wordpress.org/support/users/andrezrv/)
 * Last activity: [12 years, 8 months ago](https://wordpress.org/support/topic/got-some-code-for-you/#post-4180533)
 * Status: resolved