Archived topic
wp_get_attachment_metadata stopped working?
4 replies · Started by Alexander on December 9, 2020
Hello,
with your previous help, I've added some bells & whistles to my attachment pages. Without getting into too much detail, this has stopped working and I'm scratching my head about what's going on.
Here's a part of the Hook Element:
<?php
global $post;
$photo_title = single_post_title('',false);
$faa_url = get_post_meta( $post->ID, 'store_link', true );
$metadata = wp_get_attachment_metadata();
$photo_id = wp_basename($metadata['file'],'.jpg');
It appears that retrieving the $metadata via wp_get_attachment_metadata() isn't working anymore - the $photo_id (which is the base filename of the image) is empty; here's what this looks like now: https://www.alex-kunz.com/the-bluest-hour-solana-beach/tabletop-blue-solana-beach/
In this link similarly, the Hook Element that I was using to show the width and height of the photo in the footer also stopped working (the text now reads "This photo appears in The Bluest Hour, Solana Beach at × pixels" when it should be "This photo appears in The Bluest Hour, Solana Beach at WIDTH × HEIGHT pixels").
<?php
global $post;
$metadata = wp_get_attachment_metadata();
printf( __( 'This photo appears in <b><a href="%4$s" title="View %5$s" rel="gallery">%6$s</a></b> at <a href="%1$s" title="Link to full-size image">%2$s × %3$s</a> pixels<br>', 'generatepress' ),
esc_url( wp_get_attachment_url() ),
$metadata['width'],
$metadata['height'],
esc_url( get_permalink( $post->post_parent ) ),
esc_attr( strip_tags( get_the_title( $post->post_parent ) ) ),
get_the_title( $post->post_parent )
);
This also relies on wp_get_attachment_metdata.
I have no idea what's going on and would greatly appreciate your help.
Thanks!
Alex.
Did WordPress 5.6 perhaps change wp_get_attachment_metadata? I found this https://core.trac.wordpress.org/ticket/50679 but don't understand what it means.
Hi there,
I'm afraid I'm not sure - I haven't followed the changes they've made there.
What's the first snippet doing, exactly?
As for the second snippet, have you debugged the variables to make sure the width and height are coming through?
Thanks, Tom.
For whatever reason, the default for the ID (per https://developer.wordpress.org/reference/functions/wp_get_attachment_metadata/), doesn't work anymore for wp_get_attachment_metdata() in WordPress 5.6 - after I changed it to wp_get_attachment_metdata($post->ID) it's working again as expected.
Cheers
Alex.
Glad you got it sorted :)