Home › Forums › General Issues › Get image alt text and write it to a custom field › Reply To: Get image alt text and write it to a custom field
There are 2 ways to do this
1) Using PHP. Doing it with PHP would mean that you’d need to update the post to load the alt text and then update a second time.
You would use an acf/prepare_field (https://www.advancedcustomfields.com/resources/acf-prepare_field/) or acf/load_value (https://www.advancedcustomfields.com/resources/acf-load_value/) filter. In this filter you would get the image field, get the image alt tag and set the field value.
2) The second way is using custom JS and AJAX. This is more complex, but probably what you’re looking for.
First you’d need to add custom JavaScript. In this script you would have an action triggered when an image field is changed. In your action you would use ACFs JS APi (https://www.advancedcustomfields.com/resources/javascript-api/) to get the value of the image field if there is no text in the text field. There are not a lot of example of doing anything like this. This example shows how to do this with 2 select fields, but it has the basics in it. https://github.com/Hube2/acf-dynamic-ajax-select-example/tree/master/dynamic-select-example
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.