Home › Forums › General Issues › Attachment field – how to output in template?
I have an attachment field named “Copyright”, i get the information e. g. in my ACF galleries with get_post_meta( $image['ID'], 'copyright', true )
.
Now i want to display this information on the images that the user puts in to a normal WYSIWYG field. How can i do this? With a filter?
Hi @herrfischer
Perhaps you can use this: https://codex.wordpress.org/Function_Reference/wp_generate_attachment_metadata
Okay i will try that, but as far as I know now, after googling a lot, the only way is to put it in the caption field, because otherwise wordpress will not put html to the image. I found one (very old) plugin that does it this way.
I thought there could be a ACF function or something. Now I did it with a normal custom field and put it to the caption. For this I had to disable the caption and made a custom caption:
// 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),
"helps" => __("(Das \"©\" Zeichen wird automatisch hinzugefügt)"),
);
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)) {
$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);
This is the output:
<div id="attachment_1312" style="width: 310px" class="wp-caption alignleft">
<img class="alignleft size-medium wp-image-1312" src="http://localhost:8888/mywebsite/wp-content/uploads/2015/09/download102-300x225.jpg" alt="download10" height="225" width="300">
<p class="wp-caption-text">
<span class="caption-customfield">© Name of Photographer</span>
</p>
</div>
I ended up using the regular caption field.
It is no problem to show the attachment custom field under article thumbnails or gallery images, but if you put this field to the regular caption of images used to put in the tinyMCE there are some drawbacks:
– you have to do “disable_captions” (and then generate your own …) – that deletes the regular caption field from the editor 🙁
– the ACF attachment meta field is not available in the image editor, you have to go the library and edit the image information there to add content to the ACF field
This is confusing I think. Now i added the regular caption to the gallery and the post thumbnails.
That’s some extensive work you’ve done. Thanks a lot for sharing your findings.
Sorry you couldn’t get it working to your satisfaction but as I’m sure you know this has more to do with WP cores restrictions than ACF 🙂
Best of luck in your project!
Oh I’m quite happy with the solution. Only drawback is that the customer has to type a “©” when he wants to put copyright information, but I think that shouldn’t be a big deal 😉 .
The code above is more like copy pasted from here and there, fiddling around and see what happens, but when i reach a point at which I know there is no better way than standard wordpress/ACF solutions I am mostly happy with that because I know it is the best possible solution, and … I learned a little, maybe it helps in other situations in future 😉
The topic ‘Attachment field – how to output in template?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.