Home › Forums › General Issues › Using ACF field as Image Caption
Does anyone know if this is possible: I’d like to create an ACF field for Photo Credit. I would assign that field to attachments, so that when I upload an image, I can select either a WP user (ACF User Field) or the photographer isn’t a WP user add some text (ACF Text field) denoting the name of the photographer.
What I’d like to do is then automatically add that below a photo when there are values. I’m having trouble getting that to work. I think what I want is the img_caption_shortcode filter. Something like:
add_filter(‘img_caption_shortcode’, ‘caption_shortcode_with_photog’, 10, 3);
But I haven’t managed to get it to work.
Might anyone know how this could be done who could steer me in the right direction?
Yup, you’re right – you can utilize
img_caption_shortcode! The example from the WordPress codex is a good skeleton structure to work off of to add the markup you’d like to.
Something to keep in mind is that you’ll need to pass a second parameter when getting your
photo_credit field to target the media it is attached to.
One way of getting the appropriate ID is to use preg_match & regex to split the number off before the
$attr are defined in that example:
preg_match( '/(\d+)$/', $attr['id'], $matches ); $media_id = $matches; $credit = get_field( 'your_field_name', $media_id );
From there you can add conditionals how you’d like & place it where you’d like within your markup. Let me know how it pans out!
Exactly what I searched for but unfortunately neither image_send_to_editor nor img_caption_shortcode seems to work with Gutenberg. Is there any known option to use the ACF fields in media files with Gutenberg (except developing own blocks)?
I’m looking exactly for that.
Have you found any solution, please?
Thank you in advance!
I developed my own block, but I don’t recommend as Gutenberg evolves much to fast.
I think ACF Blocks is the best solution. You should be able to create your own block and save it as preset without writing a single line of code. I also plan to replace my blocks with ACF Blocks.
Thank you ))
You should be able to create your own block and save it as preset without writing a single line of code. I also plan to replace my blocks with ACF Blocks. kiss anime
Better to create your own block and keep it as preset without inluding and writnig a line of code? I did same on best 4 slice toaster oven 2021
You must be logged in to reply to this topic.
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’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.