If you are having a notice about writing the .mo files please read this doc

Hi, Amit here, I am the WPML Support Manager, our current ticket queue is high, update your WPML plugins and make sure you meet the minimal requirements for running WPML before reporting an issue please - many tickets are resolved doing that

Please look at our updated list of Known Issues and you can also use our support search to find helpful information and of course review our documentation before opening a ticket.

If you do need to open a ticket please make sure to provide us with all the needed information as described in this page

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 4 replies, has 2 voices.

Last updated by Andrés 4 months ago.

Assigned support staff: Andrés.

Author Posts
July 18, 2019 at 8:57 pm #4242299

usahello

I'm trying to remove any EXIF metadata that is part of a photograph that I upload to the Media Library because it gets assigned to the alt, title, and caption fields of the image, which we don't want to happen. It's not a big hassle to delete these fields for the native language, but because WPML duplicates the image for all translated languages, it quickly becomes cumbersome when we have 5+ languages. This is why we're trying to do this programmatically upon image upload.

I've managed to remove all the EXIF data for the default site language using this code:

function remove_image_metadata_on_upload( $post_ID ) {

  // Check if uploaded file is an image, else do nothing
  if ( wp_attachment_is_image( $post_ID ) ) {

      $image_metadata = array(
        'ID'        		=> $post_ID,  // the image (ID) to be updated
        'post_title'    => '',     		// Set image Title
        'post_excerpt'  => '',    		// Set image Caption (Excerpt)
        'post_content'  => ''   			// Set image Description (Content)
      );

      // Remove the image Alt text
      delete_post_meta( $post_ID, '_wp_attachment_image_alt' );

      // Set the image metadata (e.g. Title, Excerpt, Content) using the array values above
      wp_update_post( $image_metadata );

  } 

}
add_action( 'add_attachment', 'remove_image_metadata_on_upload' );

I take a similar approach for the translated images by using WPML's `wpml_media_create_duplicate_attachment` hook like so:

function remove_image_metadata_on_duplication( $post_id, $duplicate_post_id ) {

  $image_metadata = array(
        'ID'        		=> $duplicate_post_id,  // the image (ID) to be updated
        'post_title'    => '',     		// Set image Title
        'post_excerpt'  => '',    		// Set image Caption (Excerpt)
        'post_content'  => ''   			// Set image Description (Content)
      );

      // Remove the image Alt text
      delete_post_meta( $duplicate_post_id, '_wp_attachment_image_alt' );

      // Set the image metadata (e.g. Title, Excerpt, Content) using the array values above
      wp_update_post( $image_metadata );

}
add_action( 'wpml_media_create_duplicate_attachment', 'remove_image_metadata_on_duplication', 10, 2 );

However, I'm getting PHP memory errors and the image fails to upload for the native language and all translations. It seems to be something with the `wp_update_post` method for the duplicated image because when I remove that line of the code everything works properly, all metadata is removed for the native language and the alt text is removed for the translated duplicate.

PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 73728 bytes) in /italiahello-www/wp-includes/functions.php on line 5910

PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 20480 bytes) in /italiahello-www/wp-includes/wp-db.php on line 1973

I also tried using the `wpml_after_duplicate_attachment` WPML hook, but experienced similar results. I know that using the `wp_update_post` method can potentially lead to infinite loops if it's used with the `save_post` action, requiring you to remove your hook before using `wp_update_post` and then re-adding it once you're done. However, I couldn't discern if that was what is happening here.

If you have any thoughts I'm very interested to hear them. Thanks.

July 19, 2019 at 1:14 pm #4246675

Andrés
Supporter

Languages: English (English ) Spanish (Español ) French (Français )

Timezone: Europe/Paris (GMT+01:00)

Hello there,

Welcome to WPML support forum.

I'm afraid that what you are looking for is beyond of the scope of our support as it is custom code.

However, if you want to avoid that WPML duplicates your future images, you can do the following:
- Go to WPML-> Settings -> Media Translation.
- How to handle media for new content:
- Uncheck the following options:
o When uploading media to the Media library, make it available in all languages
o Duplicate media attachments for translations
o Duplicate featured images for translations

Documentation:
https://wpml.org/documentation/getting-started-guide/media-translation/

Best regards,
Andrés

July 19, 2019 at 3:43 pm #4247193

usahello

Well, we want the media to be duplicated so that it shows up for our translated content and so we can translate the alt, title, and caption text as needed. So, I don't know if we want to disable the media duplication.

I understand that what I posted was custom code (which I've seen other people do and have seen your support staff respond), but if you don't/can't reply to my specific code, can you at least speak to the use of the `wpml_media_create_duplicate_attachment` hook and if that would be a suitable place to try and accomplish what we're trying to do? Is there any other place in your codebase where we can update the metadata of media files after they've been duplicated by WPML so we can change the alt, title, and caption fields?

July 20, 2019 at 6:33 pm #4250449

usahello

I'll figure out another way to accomplish this, thank you.

July 22, 2019 at 5:30 am #4254055

Andrés
Supporter

Languages: English (English ) Spanish (Español ) French (Français )

Timezone: Europe/Paris (GMT+01:00)

I'm glad to know you find a solution! 🙂
Have a nice day.