[gallery] shortcode with attribute type="carousel" in order to show a Bootstrap Carousel based on the selected images and their titles and descriptions. Very important: this plugin assumes either your theme includes the necessary Bootstrap Javascript and CSS files to display the carousel properly, or that you have included the files on your own. It will not include the files for you, so if they are not present, the carousel will not work.
Author: Andrés Villarreal
Version: 0.1
*/
remove_shortcode( 'gallery', 'gallery_shortcode' ); /* Remove original shortcode */
add_shortcode( 'gallery', 'agnosia_bootstrat_carousel_gallery_shortcode' ); /* Add custom shortcode */
function agnosia_bootstrat_carousel_gallery_shortcode( $attr ) {
/* Validate for necessary data */
if ( isset( $attr['ids'] ) and isset( $attr['type'] ) and $attr['type'] == 'carousel' ) :
/* Define data by given attributes. */
$ids = $attr['ids'];
$name = isset( $attr['name'] ) ? sanitize_title( $attr['name'] ) : 'agnosia-bootstrap-carousel' ; /* Any name. String will be sanitize to be used as HTML ID. Recomended when you want to have more than one carousel in the same page. Default: agnosia-bootstrap-carousel. */
$indicators = isset( $attr['indicators'] ) ? $attr['indicators'] : 'before-inner' ; /* Accepted values: before-inner, after-inner, after-control, false. Default: before-inner. */
$control = isset( $attr['control'] ) ? $attr['control'] : 'true' ; /* Accepted values: true, false. Default: true. */
$interval = isset( $attr['interval'] ) ? $attr['interval'] : 5000 ; /* The amount of time to delay between automatically cycling an item. If false, carousel will not automatically cycle. */
$pause = isset( $attr['pause'] ) ? $attr['pause'] : 'hover' ; /* Pauses the cycling of the carousel on mouseenter and resumes the cycling of the carousel on mouseleave. */
/* Obtain HTML. */
$output = agnosia_bootstrat_carousel_get_html_from( $ids , $name , $indicators , $control , $interval , $pause );
/* If attributes could not be validated, execute default gallery shortcode function */
else : $output = gallery_shortcode( $attr ) ;
endif;
return $output;
}
function agnosia_bootstrat_carousel_get_html_from( $ids , $name , $indicators , $control , $interval , $pause ) {
/* Obtain posts array by given ids. Then construct HTML. */
$images = agnosia_bootstrat_carousel_make_array( $ids );
$output = '';
if ( is_array( $images ) and !empty( $images ) ) :
$posts = array();
foreach ( $images as $image_id ) :
$posts[] = get_post( intval( $image_id ) , ARRAY_A );
endforeach;
if ( is_array( $posts ) and !empty( $posts ) ) :
$output = agnosia_bootstrat_carousel_make_html_from( $posts , $name , $indicators , $control , $interval , $pause );
endif;
endif;
return $output;
}
function agnosia_bootstrat_carousel_make_html_from( $posts , $name , $indicators , $control , $interval , $pause ) {
/* The important stuff happens here! */
/* Initialize carousel HTML. */
$output = '