Skip to content

Instantly share code, notes, and snippets.

@pommiegranit
Created July 31, 2014 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pommiegranit/739019f4d8494ec965c2 to your computer and use it in GitHub Desktop.
Save pommiegranit/739019f4d8494ec965c2 to your computer and use it in GitHub Desktop.
Better Images
[caption id="attachment_1041" align="alignnone" width="690"]
<img src="http://www.test.dev/wp-content/uploads/2013/03/spider-man-690x1024.jpg"
alt="The Amazing Spider Man" width="690" height="1024" class="size-large wp-image-1041"
data-desc-style="map"/>
Click on the spider icon to learn more about The Amazing Spider Man[/caption]
[caption id="attachment_1042" align="alignnone" width="620"]
<img src="http://www.test.dev/wp-content/uploads/2013/03/the-dark-knight-rises.jpg"
alt="The Dark Knight Rises" width="620" height="935" class="wp-image-1042 size-full"
data-desc-style="box-overlay" data-desc-reveal="fadein" data-desc-trigger="inview" data-points="10px,10px,60%"/>
The Dark Knight Rises[/caption]
[caption id="attachment_1039" align="alignnone" width="694"]
<img src="http://www.test.dev/wp-content/uploads/2013/03/ironman-2-694x1024.jpg"
alt="Iron Man 2" width="694" height="1024" class="size-large wp-image-1039"
data-desc-style="box-overlay" data-desc-reveal="fadein" data-desc-trigger="click" data-points="10px,10px,60%"/>
Click on the image to learn more about Iron Man 2[/caption]
<?php
/*
Plugin Name: Better Images
Plugin URI: http://premium.wpmudev.org/
Description: Better images
Author: Chris Knowles
Version: 1.0
Author URI: http://twitter.com/ChrisKnowles
*/
function bi_img_caption_shortcode( $output, $attr, $content ) {
extract(shortcode_atts(array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => '',
), $attr));
if ( 1 > (int) $width || empty($caption) )
return $content;
// Grab ID to query post_content [description] field for image
preg_match('/([\d]+)/', $id, $matches);
$description = '';
// Do we need to get description?
if ( strpos( $content , 'data-desc-style' ) ) {
if ($matches[0]) {
global $wpdb;
$custom_description = $wpdb->get_row("SELECT post_content FROM $wpdb->posts WHERE ID = {$matches[0]};");
if ($custom_description->post_content) {
$description = '<div class="wp-image-description">' . apply_filters('the_content', $custom_description->post_content) . '</div><div class="wp-desc-placeholder"></div>';
}
}
if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
}
return '<div ' . $id . ' class="wp-caption ' . esc_attr($align) . '">'
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p>' . $description . '</div>';
}
add_filter( 'img_caption_shortcode', 'bi_img_caption_shortcode', 1, 3 );
function bi_enqueue_scripts() {
wp_enqueue_style( 'bimages-style', plugins_url() . '/better-images/better-images.css' );
wp_enqueue_script( 'bimages-script', plugins_url() . '/better-images/better-images.js', array(), null, true );
wp_enqueue_script( 'inview', plugins_url() . '/better-images/jquery.inview.js', array(), null, true );
}
add_action( 'wp_enqueue_scripts' , 'bi_enqueue_scripts' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment