いつの頃からか、 WordPress で写真を貼り付けるとキャプションを付けることが出来るようになってました。小洒落た機能で大好きなんですが、これは WordPress のショートコードって機能を使って実現しているものなんだそうです。
で、先日のレンズプロテクターが割れた時のエントリを書いた時に、写真の EXIF を表示したかったので、適当なプラグインを探していたところ WPExifView というプラグインがちょうど俺のニーズに合ってたっぽかったのでこれを使おうと思ったのですが、キャプションに使おうと思っても使えない…。というのはショートコード内でショートコードをネストして実行するにはショートコード側の対応が必要っぽいからなんですね。場当たり的に弄ってもよかったんですが、 WordPress 本体を弄るとバージョンアップ時に泣きだす未来が垣間見えたので、だったら自作するべー、とプラグインを作ってみました。
使い方は WordPress で写真を貼って、キャプションを書いて、テキスト表示モードにして[caption][/caption]ってのを[exifcaption][/exifcaption]とするだけ。具体的にはこう。

[exifcaption id=”attachment_2419″ align=”aligncenter” width=”300″]
こっちが [exifcaption][/exifcaption][/exifcaption]
表示する EXIF は決め打ちなので、変更したい人はソースを弄ってください。
ということでソースはこんな感じ。 WordPress 本体の Caption を改造したコードなので残念ながら GPL2 でございます。使いたい人がいたらご自由にどーぞ。
<?php
/*
Plugin Name: EXIFCaption
Plugin URI:
Description: Caption with base image EXIF
Version: 0.1
Author: unknown
Author URI: ounknown
License: GPL2
*/
add_shortcode('exifcaption', 'img_exifcaption_shortcode');
function img_exifcaption_shortcode($attr, $content = null) {
// New-style shortcode with the caption inside the shortcode with the link and image tags.
if ( ! isset( $attr['exifcaption'] ) ) {
if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
$content = $matches[1];
$attr['exifcaption'] = trim( $matches[2] );
}
}
if ( ! isset( $attr['imgfile'] ) ) {
if ( preg_match( '#href="(.*?)"#is', $content, $matches ) ) {
$attr['imgfile'] = trim( $matches[1] );
}
}
// Allow plugins/themes to override the default caption template.
$output = apply_filters('img_exifcaption_shortcode', '', $attr, $content);
if ( $output != '' )
return $output;
extract(shortcode_atts(array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'imgfile' => '',
'exifcaption' => ''
), $attr, 'exifcaption'));
if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => get_the_ID() );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
if (wp_get_attachment_url( $attachment->ID , false ) == $attr['imgfile']) {
if ( is_callable( 'exif_read_data' ) ) {
$exif = exif_read_data(get_attached_file($attachment->ID), 0, true);
}
break;
}
}
}
if ( isset ($exif["EXIF"])) {
list( $n, $d ) = explode( '/', $exif["EXIF"]["FNumber"] );
if ( !empty($d) )
$exif["EXIF"]["FNumber"] = $n / $d;
$exifstr = "<pre style=\"color: white; background: rgba(0,0,0,0.6); display:block; border: 0px; padding: 2px; margin: 0px;margin-right: 8px;box-shadow: 0px 0px 7px rgb(119, 119, 119);\"><small>".
"Manufacturer : " . $exif["IFD0"]["Make"]."\n".
"Model Name : " . $exif["IFD0"]["Model"]."\n".
"Date Time : " . $exif["IFD0"]["DateTime"]."\n".
"Exposure Time : " . $exif["EXIF"]["ExposureTime"]."\n".
"Focal Length : " . (float)$exif["EXIF"]["FocalLength"]."mm"."\n".
"F Number : " . $exif["EXIF"]["FNumber"]."\n".
"ISO : " . $exif["EXIF"]["ISOSpeedRatings"].
"</small></pre>";
}
if ( 1 > (int) $width || (empty($exifcaption) && empty($exifstr)) )
return $content;
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
. do_shortcode( $content ) . $exifstr. '<p>' . $exifcaption .'</p></div>';
}
?>

コメントを残す