John Davidson

php - Wordpress add_attachment hook not returning metata

0 comments
Message:


I am trying to hook into WordPress after the file is uploaded and metadata is created. Currently I am using the following code.


add_action( 'add_attachment', array($this,'ProcessFile') );

public function ProcessFile( $attachment_id )
{
$Images = wp_get_attachment_metadata( $attachment_id );
}

However, $Images returns false which means the metadata has not been created. What hook should I be using to get the metadata of the image?



My Answer:
You should use the `add_attachment` hook to get the metadata of the image after it has been uploaded. However, the issue might be that the metadata is not immediately available after the file is uploaded.

You can try using the `wp_generate_attachment_metadata` hook instead, which is called after the metadata has been generated for the attachment. Here is an example:

php
add_action( 'wp_generate_attachment_metadata', array($this,'ProcessFile'), 10, 2 );

public function ProcessFile( $metadata, $attachment_id ) {
// Do something with the metadata
}


This hook passes the metadata and the attachment ID as parameters, so you can access the metadata of the image after it has been generated.

Rate this post

3 of 5 based on 3887 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us