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); 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 '
' . do_shortcode( $content ) . the_media_credit_html($credit_id) . '

'. $attr['caption'] . '

' . '
'; } else{ return '
' . do_shortcode( $content ) . '

' . $attr['caption'] . '

' . '
'; } } 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 with an ID in content preg_match_all( '/wp-image-[0-9]+/', $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); $attachment_id = $the_id[0]; $attachment_id = str_replace('wp-image-', '', $attachment_id); //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! /*matches

*/ if(preg_match('/

\<\/a><\/p>/', $content)){ $pattern = '/

\<\/a><\/p>/'; $new_image = '

' . $image_credit . '
'; } /*matches

*/ else if(preg_match('/

\<\/a><\/p>/', $content)){ $pattern = '/

\<\/a><\/p>/'; $new_image = '

' . $image_credit . '
'; } /*matches

*/ else if(preg_match('/\<\/a><\/p>/', $content)){ $pattern = '/\<\/a><\/p>/'; $new_image = '
' . $image_credit . '

'; } /*matches

*/ else if(preg_match('/\<\/a><\/p>/', $content)){ $pattern = '/\<\/a><\/p>/'; $new_image = '
' . $image_credit . '

'; } /*matches */ else if(preg_match('/\<\/a>/', $content)){ $pattern = '/\<\/a>/'; $new_image = '
' . $image_credit . '
'; } /*matches */ else if(preg_match('/\<\/a>/', $content)){ $pattern = '/\<\/a>/'; $new_image = '
' . $image_credit . '
'; } /*matches

*/ else if(preg_match('/\

\(.*?)/', $content)){ $pattern = '/\

\(.*?)/'; $new_image = '

' . $image_credit . '
'; } /*matches

*/ else if(preg_match('/\

\(.*?)/', $content)){ $pattern = '/\

\(.*?)/'; $new_image = '

' . $image_credit . '
'; } /*matches

*/ else if(preg_match('/\\(.*?)/', $content)){ $pattern = '/\\(.*?)/'; $new_image = '
' . $image_credit . '

'; } /*matches

*/ else if(preg_match('/\\(.*?)/', $content)){ $pattern = '/\\(.*?)/'; $new_image = '
' . $image_credit . '

'; } /*matches
*/ else if(preg_match('/\(.*?)\<\/div>/', $content)){ $pattern = '/\(.*?)\<\/div>/'; $new_image = '${4}'; } /*matches */ else if(preg_match('/\/', $content)){ $pattern = '/\/'; $new_image = '
' . $image_credit . '
'; } /*matches */ else{ $pattern = '/\/'; $new_image = '
' . $image_credit . '
'; } $content = preg_replace($pattern, $new_image, $content); endif; } } return $content; } add_filter('the_content', 'filter_images'); /** * Enqueue the styles. These are quite minimal. * @donaldG */ function media_credit_stylesheet() { wp_enqueue_style( 'media-credit', MEDIA_CREDIT_URL . 'css/media-credit.min.css', array(), 1.0, 'all'); } add_action('wp_print_styles', 'media_credit_stylesheet'); /** * Custom Stylesheet added to the WP Admin CSS in the header of the page to help out the display of * the ACF fields * @donaldG */ function media_load_custom_wp_admin_style() { wp_register_style( 'custom_wp_admin_css', MEDIA_CREDIT_URL . 'css/media-credit-admin-style.min.css', false, '1.0.0' ); wp_enqueue_style( 'custom_wp_admin_css' ); } add_action( 'admin_enqueue_scripts', 'media_load_custom_wp_admin_style' );