• Resolved Tim Trott

    (@azuliadesigns)


    I’m trying to amend image attachment meta on upload by hooking into the wp_generate_attachment_metadata filter:

    add_filter('wp_generate_attachment_metadata','updateExif_uploaded_image');

    function updateExif_uploaded_image($image_data)
    {
      // do stuff ...
      wp_update_attachment_metadata( $attachment_id, $image_data);
      return $image_data;
    }

    While “do stuff” works, I’m having trouble actually updating/saving the meta data as from what I can see, the attachment id is not passed into this function.

    From /wp-admin/includes/image.php I can see that the ID is passed as a parameter but how do I get that from my function?

    I tried function updateExif_uploaded_image($image_data, $attachment_id) but this caused an error with incorrect number of parameters.

    Thanks in advance

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    add_filter('wp_generate_attachment_metadata','updateExif_uploaded_image', 10, 2 );
    function updateExif_uploaded_image($image_data, $attachment_id)  {
      // do stuff ...

    The final parameter 2 in add_filter() is what sets the number of parameters. It defaults to 1 so is often omitted in examples. The 10 is a priority parameter. The default is 10, we use it here just to get to the 4th parameter.

    Thread Starter Tim Trott

    (@azuliadesigns)

    :facepalm:

    Thanks @bcworkz, worked first time! Can’t believe I missed that! I spent ages trying to work out why it wasn’t working, all it needed was another pair of eyes!

    Thanks again

    Cheers and Merry Christmas! ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get attachment id within wp_generate_attachment_metadata filter’ is closed to new replies.