Skip to content

Instantly share code, notes, and snippets.

@shankie-codes
Last active August 29, 2015 14:16
Show Gist options
  • Save shankie-codes/e74b9c9157b65d93f6d1 to your computer and use it in GitHub Desktop.
Save shankie-codes/e74b9c9157b65d93f6d1 to your computer and use it in GitHub Desktop.
Galleria ACF Repeater
<?php
/*
* Snippet to add a media gallery to post types/templates of your choosing.
* PHP include me.
* If using Proper Bear, drop me in _/inc/php and I'll work dandy
*/
// Define an array of post types, templates etc to attach this repeater to.
$locations = array(
// Possible values for 'param' include post_type, user_type, post, post_category, post_format, post_status, post_taxonomy
// page, page_type, page_parent, page_template, user_form, user_role, attachment, taxonomy, comment, widget, options_page
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
'order_no' => 0,
'group_no' => 0,
),
)
);
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_media-gallery',
'title' => 'Media Gallery',
'fields' => array (
array (
'key' => 'field_52a99ee10fb53',
'label' => 'Gallery',
'name' => 'gallery',
'type' => 'repeater',
'instructions' => 'Add images and videos to a gallery. Use the shortcode [galleria] to place the gallery.',
'sub_fields' => array (
array (
'key' => 'field_52a99f200fb54',
'label' => 'Is video',
'name' => 'is_video',
'type' => 'true_false',
'instructions' => 'Is this a video?',
'column_width' => 10,
'message' => '',
'default_value' => 0,
),
array (
'key' => 'field_52a99f5f0fb55',
'label' => 'Video',
'name' => 'video',
'type' => 'oembed',
'instructions' => 'YouTube or Vimeo url',
'conditional_logic' => array (
'status' => 1,
'rules' => array (
array (
'field' => 'field_52a99f200fb54',
'operator' => '==',
'value' => '1',
),
),
'allorany' => 'all',
),
'column_width' => 20,
'default_value' => '',
'placeholder' => '',
'prepend' => 'http://',
'append' => '',
'formatting' => 'none',
'maxlength' => '',
),
array (
'key' => 'field_52a99f9e0fb56',
'label' => 'Image',
'name' => 'image',
'type' => 'image',
'conditional_logic' => array (
'status' => 1,
'rules' => array (
array (
'field' => 'field_52a99f200fb54',
'operator' => '!=',
'value' => '1',
),
),
'allorany' => 'all',
),
'column_width' => 15,
'save_format' => 'object',
'preview_size' => 'thumbnail',
'library' => 'all',
),
array (
'key' => 'field_52a9a0340fb57',
'label' => 'Caption',
'name' => 'caption',
'type' => 'textarea',
'instructions' => 'Short description of the image or video',
'column_width' => '',
'default_value' => '',
'placeholder' => '',
'maxlength' => '',
'formatting' => 'none',
),
),
'row_min' => '',
'row_limit' => '',
'layout' => 'table',
'button_label' => 'Add an image or video',
),
),
'location' => $locations,
'options' => array (
'position' => 'normal',
'layout' => 'default',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
function galleria($container, $theme_url, $debug = 'false') {
// Prepare an array to populate before encoding as a JSON object for Galleria
$ready = array();
while(has_sub_field('gallery')){
$image = get_sub_field('image');
if(get_sub_field('video')):
// Get the iframe -- contains iframe markup
$iframe = get_sub_field('video');
// use preg_match to find iframe src
preg_match('/src="(.+?)"/', $iframe, $matches);
$src = $matches[1];
$video_service = str_ireplace('www.', '', parse_url($src, PHP_URL_HOST));;
switch (substr($video_service, 0, 5)) {
case 'youtu':
// Trim away some of ACF's oembed code
$src = rtrim($src, '?feature=oembedvideo');
// Get the video ID from the URL
$video_id = youtube_id_from_url($src);
// Get the thumbnail URL
$thumb = 'http://img.youtube.com/vi/' . $video_id . '/0.jpg';
break;
case 'vimeo':
// Way easier....
$thumb = $src . 'thumb';
break;
default:
# code...
break;
}
$ready[] = array(
'iframe' => $src,
'description' => get_sub_field('caption'),
'thumb' => $thumb
);
elseif($image):
$ready[] = array(
'image' => $image['sizes']['large'],
'thumb' => $image['sizes']['thumbnail'],
'description' => get_sub_field('caption')
);
else:
endif;
}
echo '<div id="' . $container .'"></div>' .
'<script>
var data = ' . json_encode($ready) . ';
Galleria.loadTheme("' . $theme_url . '");
Galleria.configure({
debug: ' . $debug . '
});
Galleria.run("#' . $container .'", {dataSource : data, height: 0.5625});
</script>';
}
/**
* get youtube video ID from URL
*
* @param string $url
* @return string Youtube video id or FALSE if none found.
*/
function youtube_id_from_url($url) {
$pattern =
'%^# Match any youtube URL
(?:https?://)? # Optional scheme. Either http or https
(?:www\.)? # Optional www subdomain
(?: # Group host alternatives
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com
(?: # Group path alternatives
/embed/ # Either /embed/
| /v/ # or /v/
| /watch\?v= # or /watch\?v=
) # End path alternatives.
) # End host alternatives.
([\w-]{10,12}) # Allow 10-12 for 11 char youtube id.
$%x'
;
$result = preg_match($pattern, $url, $matches);
if (false !== $result) {
return $matches[1];
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment