Skip to content

Instantly share code, notes, and snippets.

@jojonas
Last active June 17, 2016 21:05
Show Gist options
  • Save jojonas/f6f9590a596645124cb8 to your computer and use it in GitHub Desktop.
Save jojonas/f6f9590a596645124cb8 to your computer and use it in GitHub Desktop.
Wordpress plugin for [image id="123" width="300" align="left"]
<?php
/*
Plugin Name: Image Shortcode
Description: Insert an image by id. This plugin automatically fetches title, size and embedded image.
Version: 1.1
Plugin URI: https://gist.github.com/jojonas/f6f9590a596645124cb8
Author: Jonas Lieb
Author URI: http://www.jonaslieb.com
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
class ImageShortcodeImage
{
private $post = NULL;
private $meta = NULL;
private $req_width = NULL;
private $req_height = NULL;
private $req_align = NULL;
public function __construct($id) {
$this->post = get_post($id);
if ($this->isValidAttachment()) {
$this->meta = wp_get_attachment_metadata($this->post->ID);
}
}
public function isValidAttachment() {
return (!is_null($this->post) and $this->post->post_type == 'attachment');
}
public function toHtml($custom_link_attributes=NULL, $custom_img_attributes=NULL) {
if (!$this->isValidAttachment())
return '[invalid attachment id]';
$link_attributes = array();
if (!is_null($custom_link_attributes)) {
$link_attributes = array_merge($link_attributes, $custom_link_attributes);
}
array_push($link_attributes, 'href="' . wp_get_attachment_url($this->post->ID) . '"');
$img_classes = array();
array_push($img_classes, 'wp-image-' . $this->post->ID);
if (!is_null($this->req_align)) {
array_push($img_classes, 'align' . $this->req_align);
}
$dimensions = $this->getDisplayDimensions();
$img_attributes = array();
array_push($img_attributes, 'src="' . $this->getEmbedUrl($dimensions) . '"');
array_push($img_attributes, 'height="' . $dimensions['height'] . '"');
array_push($img_attributes, 'width="' . $dimensions['width'] . '"');
array_push($img_attributes, 'title="' . $this->post->post_title . '"');
array_push($img_attributes, 'alt="' . $this->post->post_title . '"');
array_push($img_attributes, 'class="' . implode(' ', $img_classes) . '"');
$html = '<a ' . implode(' ', $link_attributes) . '><img ' . implode(' ', $img_attributes) . '></a>';
return $html;
}
private function getDisplayDimensions() {
if (!is_null($this->req_width) and !is_null($this->req_height)) {
return array(
'width' => $this->req_width, 'height' => $this->req_height
);
}
if (is_null($this->req_width) and is_null($this->req_height)) {
return array(
'width' => $this->meta['width'], 'height' => $this->meta['height']
);
}
$aspect_ratio = $this->meta['width'] / $this->meta['height'];
if (!is_null($this->req_width)) {
return array(
'width' => $this->req_width, 'height' => $this->req_width / $aspect_ratio
);
}
if (!is_null($this->req_height)) {
return array(
'width' => $this->req_height * $aspect_ratio, 'height' => $this->req_height
);
}
}
private function getEmbedUrl() {
if (!isset($this->meta['sizes']))
return wp_get_attachment_url($this->post->ID);
if (is_null($dimensions))
$dimensions = $this->getDisplayDimensions();
$width = $dimensions['width'];
$height = $dimensions['height'];
$best_size_name = 'original';
foreach ($this->meta['sizes'] as $name => $size) {
if ($best_size_name == 'original') {
$best_size = $this->meta;
} else {
$best_size = $this->meta['sizes'][$best_size_name];
}
if ($height > $width) {
if ($size['height'] >= $height and $size['height'] < $best_size['height']) {
$best_size_name = $name;
}
} else {
if ($size['width'] >= $width and $size['width'] < $best_size['width']) {
$best_size_name = $name;
}
}
}
if ($best_size_name != 'original') {
$src = wp_get_attachment_image_src($this->post->ID, $best_size_name);
if ($src[3]) {
return $src[0];
}
}
return wp_get_attachment_url($this->post->ID);
}
public function parseAtts($atts) {
$this->req_width = isset($atts['width']) ? intval($atts['width']) : NULL;
$this->req_height = isset($atts['height']) ? intval($atts['height']) : NULL;
$this->req_align = isset($atts['align']) ? $atts['align'] : NULL;
}
}
function image_shortcode_insert($atts) {
$id = $atts['id'];
$image = new ImageShortcodeImage($id);
$image->parseAtts($atts);
return $image->toHtml(array(
'data-rel="lightbox-0"'
));
}
function image_shortcode_init() {
add_shortcode('image', 'image_shortcode_insert');
}
add_action('init', 'image_shortcode_init');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment