ACF Media Credit requires Advanced Custom Fields Pro to be installed & active or the free version of ACF with the ACF Repeater premium add-on installed & active.
';
$i = 1;
$total = count(get_field('media_credit', $attachment_id));
//http://www.advancedcustomfields.com/resources/repeater/
while( have_rows( 'media_credit', $attachment_id ) ) : the_row();
$image_credit .= '';
$credit = get_sub_field('credit');
$credit_link = get_sub_field('credit_link');
// If credit is linked, else just display credit
// Also checks if any subsequent empty fields have been added & if so do nothing
if($i == 1){
if (!empty($credit_link) && !empty($credit) ) {
$image_credit .= ''.$credit.'';
}
else{
$image_credit .= '' . $credit . '';
}
}
else if(!empty($credit)){
if (!empty($credit_link) && !empty($credit) ) {
$image_credit .= ' | '.$credit.'';
} else{
$image_credit .= ' | '. $credit .'';
}
}
else{
}
$image_credit .= '';
$i++;
endwhile;
$image_credit .= '';
endif;
return $image_credit;
}
}
/**
* Template tag to print the media credit as html.
* @param int|object $attachment_id is the attachment object ID
* @donaldG
*/
if(!function_exists('the_media_credit')){
function the_media_credit($attachment_id) {
echo the_media_credit_html($attachment_id);
}
}
/**
* Template tag to print the media credit as html.
* @param int|object $attachment_id is the attachment object ID
* @donaldG
*/
if(!function_exists('the_plain_media_credit')){
function the_plain_media_credit($attachment_id) {
if ( have_rows('media_credit', $attachment_id ) ) :
$image_credit = '';
$i = 1;
$total = count(get_field('media_credit', $attachment_id));
while( have_rows( 'media_credit', $attachment_id ) ) : the_row();
$image_credit .= '';
$credit = get_sub_field('credit');
$credit_link = get_sub_field('credit_link');
// If credit is linked, else just display credit
// Also checks if any subsequent empty fields have been added & if so do nothing
if($i == 1){
if (!empty($credit_link) && !empty($credit) ) {
$image_credit .= ''.$credit.'';
}
else{
$image_credit .= '' . $credit . '';
}
}
else if(!empty($credit)){
if (!empty($credit_link) && !empty($credit) ) {
$image_credit .= ' | '.$credit.'';
} else{
$image_credit .= ' | '. $credit .'';
}
}
else{
}
$image_credit .= '';
$i++;
endwhile;
$image_credit .= '';
endif;
return $image_credit;
}
}
/**
* Template tag to print the current post's thumbnail media credit as html.
* @param int|object $post Optional post ID or object of attachment. Default is global $post object.
* @donaldG
*/
if (!function_exists('the_post_thumbnail_media_credit')) {
function the_post_thumbnail_media_credit($post_id=null) {
global $post;
if ( $post_id && $post_id != 0) {
$post_thumbnail_id = get_post_thumbnail_id( $post_id ) ;
}
else{
$post_thumbnail_id = get_post_thumbnail_id( $post->ID ) ;
}
the_media_credit($post_thumbnail_id);
}
}
if ( !function_exists( 'get_the_post_thumbnail_media_credit' ) ) {
function get_the_post_thumbnail_media_credit( $post_id = null ) {
global $post;
if ( $post_id && $post_id != 0) {
$post_thumbnail_id = get_post_thumbnail_id( $post_id ) ;
} else {
$post_thumbnail_id = get_post_thumbnail_id( $post->ID ) ;
}
return the_media_credit_html( $post_thumbnail_id );
}
}
/**
* This just filters images that have the caption shortcode to grab any added classes
* on the image and apply them to the wrapping caption div
* From support: https://wordpress.org/support/topic/credit-disappears-if-class-added-to-image?replies=3#post-6726612
* Support for adding classes to un-captioned images in located in the filter_images() function
* @donaldG
*/
function filter_images_with_caption($content){
$captioned_images = array();
//find all images w/ caption
preg_match_all('/\[caption(.*?)\](.*?)\[\/caption]/', $content, $captioned_images);
if($captioned_images){
foreach($captioned_images[0] as $captioned_image){
if(preg_match('/\[caption(.*?)\](.*?) '',
'align' => 'alignnone',
'width' => '',
'caption' => '',
//class is not native, it's interpreted from the filter_images_with_caption function above
'class' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
$credit_id = $attr['id'];
$credit_id = str_replace('attachment_', '', $credit_id);
//adding 'has-caption' to img tag so we can exclude from regex later on
$content = preg_replace('/(.*?)class\=\"(.*?)\"(.*?)/', '${1}class="has-caption ${2}"${3}', $content );
if(current_theme_supports( 'html5') && the_media_credit_html($credit_id) && get_field('media_credit', $credit_id) ){
return ''
. do_shortcode( $content )
. the_media_credit_html($credit_id)
. ''. $attr['caption'] . ''
. '';
}
else if( the_media_credit_html($credit_id) && get_field('media_credit', $credit_id) ){
return '
';
}
}
add_filter( 'img_caption_shortcode', 'filter_img_caption_shortcode', 10, 3 );
/**
* Filters the content for images and returns a wrapping div w/ the media credit following the image
* or image link if it exists.
* @donaldG & Alicia (@bedsheet)
* Thank you Alicia for helping me get started w/ the regex & fixing my loop!
*/
function filter_images($content){
$images = array();
// Find all images in content
preg_match_all( '/\/', $content, $images );
if ($images) {
// For each image build the credit and add to content
foreach ($images[0] as $image) {
//1.2.0 updates in case there are any other stray numbers that would've been picked up, was previously just [0-9]+
$get_id = preg_match('/wp-image-[0-9]+/', $image, $the_id);
//2.0.0 updates new ways of capturing the classes & pertanent info!
//get alignment class
$get_alignclass = preg_match('/\/', $image, $align_class);
//get other classes
$get_classes = preg_match('/\/', $image, $classes);
//get width
$img_width = preg_match('/\/', $image, $img_widths);
$attachment_id = $the_id[0];
$attachment_id = str_replace('wp-image-', '', $attachment_id);
if(!empty($align_class[3])){
if(strpos($align_class[3],'align') === false){
$align_class[3] = 'align'.$align_class[3];
}
$align_class = $align_class[3];
}
if(!empty($classes[3])){
if(strpos($classes[3],'size') === false){
$classes[3] = 's'.$classes[3];
}
$classes = $classes[3];
}
if(!empty($img_widths[2])){
$img_widths = $img_widths[2];
}
//http://www.advancedcustomfields.com/resources/repeater/
if ( have_rows('media_credit', $attachment_id ) ) :
$image_credit = '';
$i = 1;
$total = count(get_field('media_credit', $attachment_id));
while( have_rows( 'media_credit', $attachment_id ) ) : the_row();
$image_credit .= '';
$credit = get_sub_field('credit');
$credit_link = get_sub_field('credit_link');
// If credit is linked, else just display credit
// Also checks if any subsequent empty fields have been added & if so do nothing
if($i == 1){
if (!empty($credit_link) && !empty($credit) ) {
$image_credit .= ''.$credit.'';
}
else{
$image_credit .= '' . $credit . '';
}
}
else if(!empty($credit)){
if (!empty($credit_link) && !empty($credit) ) {
$image_credit .= ' | '.$credit.'';
} else{
$image_credit .= ' | '. $credit .'';
}
}
else{
}
$image_credit .= '';
$i++;
endwhile;
$image_credit .= '';
// Find image with our ID
// This may seem like a lot but we need to account for images wrapped in a
tag or if for some reason autop is turned off
// If things look broken it's b/c both images are in the same paragraph tag, they shouldn't be. So, don't do that!
//exclude images with captions
if(preg_match('/\/', $content)){
$pattern = '/\/';
$new_image = '';
}
//matches