Skip to content

Instantly share code, notes, and snippets.

@panoslyrakis
Created April 4, 2017 16:51
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 panoslyrakis/83fce1be1d3dc94886845f5ab654c4c2 to your computer and use it in GitHub Desktop.
Save panoslyrakis/83fce1be1d3dc94886845f5ab654c4c2 to your computer and use it in GitHub Desktop.
Adds a shortcode that inserts responive images
<?php
/**
* Plugin Name: WPMUDEV Responsive Images
* Version: 1.0
* Description: Adds a shortcode that inserts responive images
* Author: Panos Lyrakis (WPMU DEV)
* Author URI: https://premium.wpmudev.org/profile/panoskatws
* Plugin URI: https://premium.wpmudev.org/
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
if( ! class_exists( 'WPMUDEV_Responsive_Images ') ){
class WPMUDEV_Responsive_Images{
private static $_instance = null;
private static $_default_size;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_Responsive_Images();
}
return self::$_instance;
}
private function __construct() {
self::$_default_size = apply_filters( 'WPMUDEV_Responsive_Images/default_size', 'thumbnail' );
add_shortcode( 'WPMUDEV_Responsive_Image', array( $this, 'show_image_sh' ) );
}
public static function show_images( $attachment_ids, $size = '', $return = false ){
$images = '';
if( is_numeric( $attachment_ids ) ){
$attachment_ids = array( $attachment_ids );
}
foreach( $attachment_ids as $attachment_id ){
$images .= wp_get_attachment_image( $attachment_id, $size );
}
if( $return ){
return $images;
}
echo $images;
}
public function show_image_sh( $atts ){
if( ! isset( $atts['images'] ) ){
return;
}
$atts = shortcode_atts(
array(
'images' => '',
'size' => self::$_default_size,
), $atts, 'WPMUDEV_Responsive_Image' );
$attachment_ids = explode( ',', $atts['images'] );
return self::show_images( $attachment_ids, $atts['size'] );
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['WPMUDEV_Responsive_Images'] = WPMUDEV_Responsive_Images::get_instance();
} );
}
@panoslyrakis
Copy link
Author

You can use shortcode with image id:
[WPMUDEV_Responsive_Image images="876"]
Or several image ids:
[WPMUDEV_Responsive_Image images="876,844,824"]
and also add size attribute:
[WPMUDEV_Responsive_Image images="876,844,824" size="full"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment