Support

Account

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