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 ( 1 === $i ) { if ( ! empty( $credit_link ) && ! empty( $credit ) ) { $image_credit .= '' . $credit . ''; } else { $image_credit .= '' . $credit . ''; } } elseif ( ! empty( $credit ) ) { if ( ! empty( $credit_link ) && ! empty( $credit ) ) { $image_credit .= ' | ' . $credit . ''; } else { $image_credit .= ' | ' . $credit . ''; } } else { } $image_credit .= ''; $i++; endwhile; $image_credit .= ''; //Filter to change the output of credit | credit $image_credit = apply_filters( 'acf_media_credit_base_output_html_credit', $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 ) { $image_credit = ''; if ( have_rows( 'media_credit', $attachment_id ) ) : $i = 1; 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 ( 1 === $i ) { if ( ! empty( $credit_link ) && ! empty( $credit ) ) { $image_credit .= '' . $credit . ''; } else { $image_credit .= '' . $credit . ''; } } elseif ( ! empty( $credit ) ) { if ( ! empty( $credit_link ) && ! empty( $credit ) ) { $image_credit .= ' | ' . $credit . ''; } else { $image_credit .= ' | ' . $credit . ''; } } else { } $image_credit .= ''; $i++; endwhile; $image_credit .= ''; //Filter to change the output of credit | credit $image_credit = apply_filters( 'acf_media_credit_base_output_plain_credit', $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 && 0 !== $post_id ) { $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 && 0 !== $post_id ) { $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'] . '
' . '
'; } elseif ( 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 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 ); if ( ! empty( $the_id[0] ) ) { $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 ( ! empty( $attachment_id ) && 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 ( 1 === $i ) { if ( ! empty( $credit_link ) && ! empty( $credit ) ) { $image_credit .= '' . $credit . ''; } else { $image_credit .= '' . $credit . ''; } } elseif ( ! empty( $credit ) ) { if ( ! empty( $credit_link ) && ! empty( $credit ) ) { $image_credit .= ' | ' . $credit . ''; } else { $image_credit .= ' | ' . $credit . ''; } } else { } $image_credit .= ''; $i++; endwhile; $image_credit .= ''; //Filter to change the output of credit | credit $image_credit = apply_filters( 'acf_media_credit_base_output', $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! //Filter to change the wrapper element tag $wrapper_tag = 'div'; $wrapper_tag = apply_filters( 'acf_media_credit_wrapper_tag', $wrapper_tag ); //Filter to change or add to the div class $media_credit_div_class = 'media-credit '; $media_credit_div_class = apply_filters( 'acf_media_credit_div_class', $media_credit_div_class ); //exclude images with captions if ( preg_match( '/<' . $wrapper_tag . '(.*?)class=(.*?)media-credit(.*?)/', $content ) ) { $pattern = '/<' . $wrapper_tag . '(.*?)class=(.*?)media-credit(.*?)/'; $new_image = '<' . $wrapper_tag . '${1}class=${2}media-credit${3}'; } elseif ( preg_match( '/\/', $content ) || preg_match( '/\/', $content ) ) { $pattern = '/\/'; $new_image = ''; } elseif ( preg_match( '/\\\\<\/a\>\<\/p>/', $content ) ) { $pattern = '/\\\\<\/a\><\/p>/'; $new_image = '<' . $wrapper_tag . '${5}wp-image-' . $attachment_id . '${6} ' . $media_credit_div_class . '" style="width:' . $img_widths . 'px;">' . $image_credit . '

'; } elseif ( preg_match( '/\\\<\/p>/', $content ) ) { $pattern = '/\\\<\/p>/'; $new_image = '<' . $wrapper_tag . '${2}wp-image-' . $attachment_id . '${3} ' . $media_credit_div_class . '" style="width:' . $img_widths . 'px;">' . $image_credit . '

'; } elseif ( preg_match( '/\\\<\/a\>/', $content ) ) { $pattern = '/\\\<\/a\>/'; $new_image = '<' . $wrapper_tag . '${4}wp-image-' . $attachment_id . '${5} ' . $media_credit_div_class . '" style="width:' . $img_widths . 'px;">' . $image_credit . ''; } elseif ( preg_match( '/\/', $content ) ) { $pattern = '/\/'; $new_image = '<' . $wrapper_tag . '${1}wp-image-' . $attachment_id . '${2} ' . $media_credit_div_class . '" style="width:' . $img_widths . 'px;">' . $image_credit . ''; } $content = preg_replace( $pattern, $new_image, $content ); endif; } } return $content; } add_filter( 'the_content', 'filter_images',20,3 ); add_filter( 'acf_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' );