1

I have a custom field that is an Image and the return value of the field is Image URL.

I have custom page templates where I render these image fields. Is there a way to get the meta info for the image through the image url. I do not want to change the return value to Image Object or Image ID.

  • What meta info do you need from that URL ? ( exif data , size , dimensions ) – bravokeyl May 5 '16 at 3:31
2

I can already tell that you're using ACF - if you don't want to change the return settings for the field, you can bypass ACF and just pull the image ID directly from post meta:

$image_url  = get_field( 'my_field_name' );
$image_id   = get_post_meta( $post->ID, 'my_field_name', true );
$image_meta = wp_get_attachment_metadata( $image_id );
  • Thank you. I'll give this a try. Is there a way to do this globally for all images? I did something similar (but with Image Object). I created a function where I pass the field name, which is not that big of a deal. But is there a way to get this globally - adding alt tags automatically for all images. – essjay May 5 '16 at 11:47
  • 1
    Can you explain what you mean by "get this globally"? As in, do you want a helper function that you can easily use anywhere, or do you want to somehow "hook" into the ACF get_field call (although if the latter, I question why you don't want to change the return settings). – TheDeadMedic May 5 '16 at 11:49
  • Your code doesn't work. I basically want a function I can call that will get me the alt tag f an image URL from ACF image field. – essjay May 5 '16 at 13:52
  • 1
    "Doesn't work" isn't very helpful. I'm pretty sure it's correct, but if you elaborate we can solve it together. – TheDeadMedic May 5 '16 at 14:14
  • @essjay sounds like a question for ACF's support channels – bosco May 5 '16 at 18:58
0

I needed the code in functions.php so that I could re-use it. Here's my solution below:

function get_img_alt($attachment) {    
  $post = get_post();
  $image_id  = get_post_meta( $post->ID, $attachment, true );
  $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true);
  print $image_alt;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.