WordPressのメディアギャラリーをカスタマイズする方法
WordPressのギャラリー機能をカスタマイズしたいという希望があったのでその対応をしました。
WordPressのメディアアップローダーから作成できるギャラリーの出力結果をカスタマイズする方法の備忘録です。
// メディアアップローダーのギャラリーのカスタマイズ
function add_attachment_url_to_gallery($output, $attr) {
global $post;
if (isset($attr['orderby'])) {
$attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
if (!$attr['orderby'])
unset($attr['orderby']);
}
$gallery_images = get_posts(array(
'include' => $attr['ids'],
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => $attr['orderby']
));
$output = '<div class="gallery gallery-columns-' . intval($attr['columns']) . '">';
foreach ($gallery_images as $image) {
$image_url = wp_get_attachment_url($image->ID);
$output .= '<figure class="gallery-item">';
$output .= '<a href="' . esc_url($image_url) . '" data-attachment-url="' . esc_url($image_url) . '">';
$output .= wp_get_attachment_image($image->ID, 'full');
$output .= '</a>';
$output .= '</figure>';
}
$output .= '</div>';
return $output;
}
add_filter('post_gallery', 'add_attachment_url_to_gallery', 10, 2);