Wordpress custom caption field
by herrfischerhamburg
HTML
// add copyright to caption function
//////////////////////////////////////////////////////////
// add custom field to image editor
add_filter("attachment_fields_to_edit", "add_image_copyright", 10, 2);
function add_image_copyright($form_fields, $post) {
$form_fields["copyright"] = array(
"label" => __("Copyright"),
"input" => "text",
"value" => get_post_meta($post->ID, "copyright", true),
);
return $form_fields;
}
add_filter("attachment_fields_to_save", "save_image_copyright", 10 , 2);
function save_image_copyright($post, $attachment) {
if (isset($attachment['copyright']))
update_post_meta($post['ID'], 'copyright', $attachment['copyright']);
return $post;
}
// caption function for regular caption and copyright field
// disable regular caption and build a custom one
add_filter( 'disable_captions', create_function('$a', 'return true;') );
function image_send_to_editor_2($html, $id, $caption, $title, $align, $url, $size, $alt) {
$width = 'auto';
if ( preg_match( '/width="([0-9]+)/', $html, $matches ) ) {
$width = $matches[1] . 'px';
}
// Extract attachment $post->ID
preg_match('/\d+/', $id, $att_id);
if (is_numeric($att_id[0]) && $copyright = get_post_meta($att_id[0], 'copyright', true)) {
$parts = parse_url($copyright);
$cright .= '<span class="caption-customfield">' . $copyright . '</span>';
}
$output = '[caption id="attachment_' . $id . '" align="align' . $align . '" width="' . $width . '"]';
$output .= $html;
$output .= $cright.'<span class="caption-regular">'.$caption.'</span>'.'[/caption]';
return $output;
}
add_filter('image_send_to_editor', 'image_send_to_editor_2', 10, 8);