Last active
June 18, 2018 12:09
-
-
Save w-jerome/8e6f9ae68a7190e9ef36de5bcc0e8731 to your computer and use it in GitHub Desktop.
WordPress PHP - Shortcode Gallery Images Links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getShortcodeGalleryImagesLinks ($shortcode = null) { | |
if (!is_string($shortcode)) { return false; } | |
preg_match_all('/[0-9]+/', $shortcode, $images); | |
if (!empty($images[0])) { | |
$images = array_map('intval', $images[0]); | |
} else { | |
$images = array(); | |
} | |
$imagesReturn = array(); | |
$baseUrl = get_site_url().'/wp-content/uploads/'; | |
$sizeList = get_intermediate_image_sizes(); | |
foreach ($images as $key => $attachmentID) { | |
$_image = array( | |
'meta' => array(), | |
'sizes' => array(), | |
); | |
foreach ($sizeList as $size) { | |
$imageTmp = wp_get_attachment_image_src($attachmentID, $size); | |
$_image['sizes'][$size] = array( | |
'url' => $imageTmp[0], | |
'width' => $imageTmp[1], | |
'height' => $imageTmp[2], | |
'is_intermediate' => $imageTmp[3], | |
); | |
unset($imageTmp); | |
} | |
$meta = wp_get_attachment_metadata($attachmentID); | |
$_image['meta'] = $meta['image_meta']; | |
$_image['meta']['id'] = $attachmentID; | |
$_image['sizes']['origin'] = array( | |
'url' => $baseUrl.$meta['file'], | |
'width' => $meta['width'], | |
'height' => $meta['height'], | |
'is_intermediate' => false, | |
); | |
$imagesReturn[] = $_image; | |
unset($meta); | |
unset($_image); | |
} | |
unset($baseUrl); | |
unset($sizeList); | |
unset($images); | |
return json_decode(json_encode($imagesReturn, JSON_UNESCAPED_UNICODE)); | |
} | |
$gallery = getShortcodeGalleryImagesLinks('[gallery ids="310,311,313,312"]'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment