Skip to content Skip to sidebar

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.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 -
- 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 5 replies, has 3 voices.

Last updated by Long Nguyen 1 year, 11 months ago.

Assisted by: Long Nguyen.

Author Posts
August 11, 2023 at 3:00 pm #14189467

davidB-272

SETUP:
* WPML installed on a basic WordPress 6.2 site.
* Two languages: English (default), and French.

REPRO:
* Go to Media Library -> Add New -> Upload
* Two new posts (attachments) are created in the WP database.

EXPECTED:
* WordPress "add_attachment" hook is called for *each* attachment that was added.

ACTUAL:
* add_attachment is only called for the original (English) attachment.

August 13, 2023 at 10:53 am #14192875

Ahmed Mohammed
Supporter

Timezone: Africa/Cairo (GMT+03:00)

Hi there,

Thank you for contacting WPML support. While you are waiting for one of my colleagues to take this ticket and work on it, let me provide you with the first debugging steps or if I can help with the issue quickly.

I understand that the add_attachment hook (https://developer.wordpress.org/reference/hooks/add_attachment/) is only triggered for the original language's attachment after uploading it in the media library. You believe it should be triggered for every attachment in every language.

Did I get that right?

If you let us know your goal, we can check if WPML can help with a hook (something like: https://wpml.org/wpml-hook/wpml_apply_save_attachment_actions/). If it's a bug, we can escalate it to our developer.

Looking forward to hearing back from you.

August 13, 2023 at 11:36 am #14193055

davidB-272

Hi Ahmed - that's correct! I am a developer for a plugin that uses the "add_attachment" hook to automatically generate alternative text for images when they are added. So my goal is to make sure our plugin is notified whenever new images are created so that we can call our service to generate alt text. WPML is only notifying on the original image, not any of the translations.

August 14, 2023 at 8:21 am #14195791

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi David,

Thank you for contacting WPML support, I'd be happy to help you with this issue.

I can see that issue on my demo site. I think there is a workaround by using the filter hook "wpml_object_id" to get the translation attachment ID. For example:

add_action( 'add_attachment', function( $post_id ) {
	error_log ( 'Image id: ' . $post_id );
	$translated_post_id = apply_filters( 'wpml_object_id', $post_id, 'attachment', false, 'fr');
	error_log ( 'Image id FR: ' . $translated_post_id );
});

You will see the translation attachment ID in the debug log. Refer documentation https://wpml.org/wpml-hook/wpml_object_id/

Look forward to your reply.
Thanks

August 18, 2023 at 2:53 pm #14253311

davidB-272

Hi - the problem is I do not know ahead of time the language to use. And the site may have more than one language.

Is it possible to get a list of *ALL* translations of the attachment inside the "add_attachment" handler? I tried using wpml_post_duplicates() (https://wpml.org/wpml-hook/wpml_post_duplicates/) but this just returns an empty array.

add_action( 'add_attachment', function( $post_id ) {

error_log ( 'Image id: ' . $post_id );

$translated_data = apply_filters( 'wpml_post_duplicates', $post_id );

error_log ( 'Translated IDs: ' . var_export($translated_data, true));
});

This just logs an empty array. I want to get a list of all the IDs of the translated image attachments.

August 20, 2023 at 3:03 pm #14258767

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

The filter "wpml_post_duplicates" works when you duplicate a post/page into second languages. It does not work with media items in this case.

the problem is I do not know ahead of time the language to use. And the site may have more than one language.

Then you can use the filter hook "wpml_active_languages" to get all active languages on your site and use the filter hook "wpml_object_id" above to get translation attachment ID based on the languages.
Refer to the documentation https://wpml.org/wpml-hook/wpml_active_languages/

Look forward to your reply.
Thanks

The topic ‘[Closed] add_attachment not called when media is duplicated’ is closed to new replies.