Skip to content

Instantly share code, notes, and snippets.

@Markmcl
Created July 24, 2018 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Markmcl/2bb6e97cfd03f9fee1e6bd9d76625df7 to your computer and use it in GitHub Desktop.
Save Markmcl/2bb6e97cfd03f9fee1e6bd9d76625df7 to your computer and use it in GitHub Desktop.
test source-url-to-image-captions
<?php
add_filter("attachment_fields_to_edit", "add_image_source_url", 10, 2);
function add_image_source_url($form_fields, $post) {
$form_fields["source_url"] = array(
"label" => __("Source URL"),
"input" => "text",
"value" => get_post_meta($post->ID, "source_url", true),
"helps" => __("Add the URL where the original image was posted"),
);
$form_fields["source_credit"] = array(
"label" => __("Source Credits"),
"input" => "text",
"value" => get_post_meta($post->ID, "source_credit", true),
"helps" => __("If you add credits, those will be linked instead of the whole caption."),
);
return $form_fields;
}
add_filter("attachment_fields_to_save", "save_image_source_url", 10 , 2);
function save_image_source_url($post, $attachment) {
if (isset($attachment['source_url']))
update_post_meta($post['ID'], 'source_url', trim($attachment['source_url']));
if (isset($attachment['source_credit']))
update_post_meta($post['ID'], 'source_credit', trim($attachment['source_credit']));
return $post;
}
add_filter('img_caption_shortcode', 'caption_shortcode_with_credits', 10, 3);
function caption_shortcode_with_credits($empty, $attr, $content) {
extract (shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => '',
'class' => '',
), $attr, 'caption' ));
// Extract attachment $post->ID
preg_match('/\d+/', $id, $att_id);
if (is_numeric($att_id[0]) && $source_url = get_post_meta($att_id[0], 'source_url', true)) {
if ( $source_credit = get_post_meta( $att_id[0], 'source_credit', true ) ) {
$caption .= sprintf(
' <br />Photo by: <a class="source-credit" href="%s" target="_blank" rel="noopener">%s</a>',
esc_url( $source_url ),
esc_html( $source_credit )
);
}
else {
$caption = '<a href="'. esc_url ( $source_url ) .'">'. $caption .'</a>';
}
}
if ( 1 > (int) $width || empty($caption) )
return $content;
if ( $id )
$id = 'id="' . esc_attr($id) . '" ';
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
}
<?php
add_filter("attachment_fields_to_edit", "add_image_source_url", 10, 2);
function add_image_source_url($form_fields, $post) {
$form_fields["source_url"] = array(
"label" => __("Source URL"),
"input" => "text",
"value" => get_post_meta($post->ID, "source_url", true),
"helps" => __("Add the URL where the original image was posted"),
);
$form_fields["source_credit"] = array(
"label" => __("Source Credits"),
"input" => "text",
"value" => get_post_meta($post->ID, "source_credit", true),
"helps" => __("If you add credits, those will be linked instead of the whole caption."),
);
return $form_fields;
}
add_filter("attachment_fields_to_save", "save_image_source_url", 10 , 2);
function save_image_source_url($post, $attachment) {
$old_url = get_post_meta( $post['ID'], 'source_url', true );
$old_credit = get_post_meta( $post['ID'], 'source_credit', true );
$source_url = isset( $attachment['source_url'] ) ? $attachment['source_url'] : '';
$source_credit = isset( $attachment['source_credit'] ) ? $attachment['source_credit'] : '';
if ( $source_url && $source_url != $old_url ) {
update_post_meta( $post['ID'], 'source_url', esc_url_raw( $source_url ) );
} elseif ( $old_url ) {
delete_post_meta( $post['ID'], 'source_url' );
}
if ( $source_credit && $source_credit != $old_credit ) {
update_post_meta( $post['ID'], 'source_credit', sanitize_text_field( $source_credit ) );
} elseif ( $old_credit ) {
delete_post_meta( $post['ID'], 'source_credit' );
}
return $post;
}
add_filter('img_caption_shortcode', 'caption_shortcode_with_credits', 10, 3);
function caption_shortcode_with_credits($empty, $attr, $content) {
extract (shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => '',
'class' => '',
), $attr, 'caption' ));
// Extract attachment $post->ID
preg_match('/\d+/', $id, $att_id);
if (is_numeric($att_id[0]) && $source_url = get_post_meta($att_id[0], 'source_url', true)) {
if ( $source_credit = get_post_meta( $att_id[0], 'source_credit', true ) ) {
$caption .= sprintf(
' <br />Photo by: <a class="source-credit" href="%s" target="_blank" rel="noopener">%s</a>',
esc_url( $source_url ),
esc_html( $source_credit )
);
}
else {
$caption = '<a href="'. esc_url ( $source_url ) .'">'. $caption .'</a>';
}
}
if ( 1 > (int) $width || empty($caption) )
return $content;
if ( $id )
$id = 'id="' . esc_attr($id) . '" ';
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
}
@justintadlock
Copy link

Cleaned up and working code that will also allow deletion:

add_filter( 'attachment_fields_to_edit', 'markmcl_attachment_fields_edit', 10, 2 );

function markmcl_attachment_fields_edit( $fields, $post ) {

	$fields['source_url'] = [
		'label' => __( 'Source URL' ),
		'input' => 'text',
		'value' => esc_attr( get_post_meta( $post->ID, 'source_url', true ) ),
		'helps' => __( 'Add the URL where the original image was posted.' )
	];

	$fields['source_credit'] = [
		'label' => __( 'Source Credit' ),
		'input' => 'text',
		'value' => esc_attr( get_post_meta( $post->ID, 'source_credit', true ) ),
		'helps' => __( 'If you add credits, those will be lined instead of the whole caption.' )
	];

	return $fields;
}

add_filter( 'attachment_fields_to_save', 'markmcl_attachment_fields_save', 10, 2 );

function markmcl_attachment_fields_save( $post, $attachment ) {

	$old_url    = get_post_meta( $post['ID'], 'source_url',    true );
	$old_credit = get_post_meta( $post['ID'], 'source_credit', true );

	$source_url    = isset( $attachment['source_url'] )    ? $attachment['source_url']    : '';
	$source_credit = isset( $attachment['source_credit'] ) ? $attachment['source_credit'] : '';

	if ( $source_url && $source_url != $old_url ) {
		update_post_meta( $post['ID'], 'source_url', esc_url_raw( $source_url ) );
	} elseif ( ! $source_url && $old_url ) {
		delete_post_meta( $post['ID'], 'source_url' );
	}

	if ( $source_credit && $source_credit != $old_credit ) {
		update_post_meta( $post['ID'], 'source_credit', sanitize_text_field( $source_credit ) );
	} elseif ( ! $source_credit && $old_credit ) {
		delete_post_meta( $post['ID'], 'source_credit' );
	}

	return $post;
}

add_filter( 'img_caption_shortcode', 'markmcl_img_caption', 10, 3 );

function markmcl_img_caption( $output, $attr, $content ) {

	extract( shortcode_atts( [
		'id'      => '',
		'align'   => 'alignnone',
		'width'   => '',
		'caption' => '',
		'class'   => '',
	], $attr, 'caption' ) );

	// Extract attachment $post->ID
	preg_match( '/\d+/', $id, $att_id );

	if ( is_numeric( $att_id[0] ) && $source_url = get_post_meta( $att_id[0], 'source_url', true ) ) {

		if ( $source_credit = get_post_meta( $att_id[0], 'source_credit', true ) ) {
			$caption .= sprintf(
				' <br />Photo by: <a class="source-credit" href="%s" target="_blank" rel="noopener">%s</a>',
				esc_url( $source_url ),
				esc_html( $source_credit )
			);
		} else {
			$caption = sprintf(
				'<a href="%s">%s</a>',
				esc_url( $source_url ),
				$caption
			);
		}
	}

	if ( 1 > (int) $width || empty( $caption ) ) {
		return $content;
	}

	return sprintf(
		'<div class="wp-caption %s" style="width: %spx;" %s>%s<p class="wp-caption-text">%s</p></div>',
		esc_attr( $align ),
		esc_attr( 10 + absint( $width ) ),
		$id ? 'id="' . esc_attr( $id ) . '"' : '',
		do_shortcode( $content ),
		$caption
	);
}

@justintadlock
Copy link

New version:

 add_filter( 'attachment_fields_to_edit', 'markmcl_attachment_fields_edit', 10, 2 );

 function markmcl_attachment_fields_edit( $fields, $post ) {

	$fields['source_url'] = [
		'label' => __( 'Source URL' ),
		'input' => 'text',
		'value' => esc_attr( get_post_meta( $post->ID, 'source_url', true ) ),
		'helps' => __( 'Add the URL where the original image was posted.' )
	];

	$fields['source_credit'] = [
		'label' => __( 'Source Credit' ),
		'input' => 'text',
		'value' => esc_attr( get_post_meta( $post->ID, 'source_credit', true ) ),
		'helps' => __( 'If you add credits, those will be lined instead of the whole caption.' )
	];

	return $fields;
 }

 add_filter( 'attachment_fields_to_save', 'markmcl_attachment_fields_save', 10, 2 );

 function markmcl_attachment_fields_save( $post, $attachment ) {

	$old_url    = get_post_meta( $post['ID'], 'source_url',    true );
	$old_credit = get_post_meta( $post['ID'], 'source_credit', true );

	$source_url    = isset( $attachment['source_url'] )    ? $attachment['source_url']    : '';
	$source_credit = isset( $attachment['source_credit'] ) ? $attachment['source_credit'] : '';

	if ( $source_url && $source_url != $old_url ) {
		update_post_meta( $post['ID'], 'source_url', esc_url_raw( $source_url ) );
	} elseif ( ! $source_url && $old_url ) {
		delete_post_meta( $post['ID'], 'source_url' );
	}

	if ( $source_credit && $source_credit != $old_credit ) {
		update_post_meta( $post['ID'], 'source_credit', sanitize_text_field( $source_credit ) );
	} elseif ( ! $source_credit && $old_credit ) {
		delete_post_meta( $post['ID'], 'source_credit' );
	}

	return $post;
 }

 add_filter( 'img_caption_shortcode', 'markmcl_img_caption', 10, 3 );

 function markmcl_img_caption( $output, $attr, $content ) {

	extract( shortcode_atts( [
		'id'      => '',
		'align'   => 'alignnone',
		'width'   => '',
		'caption' => '',
		'class'   => '',
	], $attr, 'caption' ) );

	// Extract attachment $post->ID
	preg_match( '/\d+/', $id, $att_id );

	if ( is_numeric( $att_id[0] ) ) {

		$source_url    = get_post_meta( $att_id[0], 'source_url',    true );
		$source_credit = get_post_meta( $att_id[0], 'source_credit', true );

		// If we have both a URL and text.
		if ( $source_url && $source_credit ) {

			$caption .= sprintf(
				' <br />Photo by: <a class="source-credit" href="%s" target="_blank" rel="noopener">%s</a>',
				esc_url( $source_url ),
				esc_html( $source_credit )
			);

		// If we don't have a URL but do have text.
		} elseif ( ! $source_url && $source_credit ) {

			$caption .= sprintf(
				' <br />Photo by: <span class="source-credit">%s</span>',
				esc_html( $source_credit )
			);

		// If we do have a URL but don't have text.
		} elseif ( $source_url && ! $source_credit ) {

			$caption = sprintf(
				'<a class="source-credit" href="%s" target="_blank" rel="noopener">%s</a>',
				esc_url( $source_url ),
				$caption
			);
		}
	}

	if ( 1 > (int) $width || empty( $caption ) ) {
		return $content;
	}

	return sprintf(
		'<div class="wp-caption %s" style="width: %spx;" %s>%s<p class="wp-caption-text">%s</p></div>',
		esc_attr( $align ),
		esc_attr( 10 + absint( $width ) ),
		$id ? 'id="' . esc_attr( $id ) . '"' : '',
		do_shortcode( $content ),
		$caption
	);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment