We have a website with images that do not contain any attachment metadata. I set out to create my own function that checks if there is metadata, and if there is not any, it will create it.
Unfortunately, I am afraid that wp_get_attachment_metadata()
stays empty all of the time. Or that I can't update the metadata with wp_update_attachment_metadata()
My function is as followed:
function custom_attach_func($attch_id) {
$meta_data = wp_get_attachment_metadata($attch_id);
if($meta_data == ''){
$attachment_path = get_attached_file($attch_id);
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attch_id, $attachment_path);
wp_update_attachment_metadata( $attach_id, $attach_data );
return $attach_data;
}else{
return wp_get_attachment_metadata($attch_id);
}
}
While wp_generate_attachment_metadata()
does generate the metadata, and wp_update_attachment_metadata()
returns true
, wp_get_attachment_metadata()
keeps returning ''
.
Is this a Wordpress bug? Am I doing it wrong?!
require_once( ABSPATH . 'wp-admin/includes/image.php' );
? Are you outside of WordPress? Could you provide code and step by step guide to reproduce the issue?wp_generate_attachment_metadata
work properly, check the codex undernotes
for this. This function is being called while injecting data to the post's content withadd_filter ('the_content', 'insertData');
wp_get_attachment_metadata()
, because the value of a meta field can be a empty string.add_filter ('the_content', 'insertData');
from the main plugin file. So it, I think, full within WordPress. It a regular Linux based (apache) webserver...custom_attach_func()
? What is the value of$attch_id
variable and how do you pass it tocustom_attach_func()
? Is it an attachement post type? And so on .... You can edit the question to add more information.