Skip Navigation

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 7 replies, has 3 voices.

Last updated by jeffH-13 2 years, 9 months ago.

Assigned support staff: Otto.

Author Posts
October 16, 2017 at 9:04 pm #1402691

FilippoZ1

Hello, I am trying to generate/update transients containing post meta when a translation is created or updated using the WPML translation editor.

Is there any API I can use for that?

an action that returns the translation post id, or any hook I can use?

I have a custom post type with really a lot of complex ACF fields, I would like to improve site performance saving all this info into a post-related transient.

I can do it on main languange with WP save_post action, but I don't know how to do it for translations when I'm using the WPML Translation Editor.

Thanks

October 17, 2017 at 2:58 pm #1403599

Otto
Supporter

Languages: English (English ) Spanish (Español )

Timezone: America/Argentina/Buenos_Aires (GMT-03:00)

Hello,

Thank you for contacting the WPML support!

Here you have our hooks library:
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/#top

I think this one may help you:
https://wpml.org/wpml-hook/wpml_object_id/

so you can get the ID of the translation and do the same for it, as original and translations are just regular WP posts.

Or this other:
https://wpml.org/wpml-hook/wpml_tm_save_translation_cf/

Let me know if this pointed you in the right direction.

Kind Regards,

Otto

October 17, 2017 at 3:48 pm #1403693

FilippoZ1

Hello Otto,
Thanx for replying.

I already know the hooks you mention, but how to fire any of these when saving a translation made with wpml translation editor?

October 17, 2017 at 5:34 pm #1403812

jeffH-13

Hello,

I have the same question. I am also trying to send data to an external store after it is updated in WordPress.

The 'save_post' hook does not appear to fire when WPML performs certain actions. For example, 'save_post' does not execute after updating the default language verison, when WPML copies content from the default language into the translated language. Is there a hook we can use that fires when WPML updates data? I need to know when translated content is updated so it can be sent to an external store.

Thanks!

October 17, 2017 at 6:33 pm #1403873

Otto
Supporter

Languages: English (English ) Spanish (Español )

Timezone: America/Argentina/Buenos_Aires (GMT-03:00)

Hello,

Please apologize, I misunderstood the issue.

I checked with our second tier support and save_post is fired also when using the Translation Editor.

Maybe Cristina's advice here point you in the right direction to debug the problem:
https://wpml.org/forums/topic/hook-for-saving-translation/#post-1316662

Let me know if it helped, please.

Kind Regards,

Otto

October 17, 2017 at 7:24 pm #1403920

jeffH-13

Hi Otto,

Thanks for the quick reply. Based on Cristina's suggestion I removed all logic from my save_post function. I added a debug statement to log post information. I still do not see the save_post action being fired for the translations being updated.

Here are my specific steps:

  • Create 2 Custom Post types
  • Add a relationship field using to one of the post types using ACF relating to the other post type
  • Set the post types as translatable, setting the relationship field as Copy
  • Add entries for each post type, including translations. Do not add relationships yet
  • Add relationship(s) to the entries in the default language
  • Note: the relationships appear in the translated language(s) correctly
  • save_post only fired for the default language, it did not fire for the translated language versions when they were updated with the relationship

Do you have any other thoughts or suggestions?

Thanks!

October 19, 2017 at 2:09 pm #1406029

jeffH-13

Note: When using the translation manager to create a new translation, I do find save_post does fire. It appears to fire before before WPML has completed its processing. i.e. apply_filters( 'wpml_post_language_details'... does not return results at this point. I tried adjusting the priority of add_action('save_post', 'my_save_post', 1000, 2); to something very high (1000) to make sure WPML has completed all of its processing. I was not successful. This is another situation where it would be nice to have a hook when WPML has finished its processing.

October 24, 2017 at 7:54 pm #1410570

jeffH-13

I did some digging into the WPML code. I found the following actions that appear to be the appropriate hooks:

add_action('wpml_after_save_post', 'publisher_save_post', 1000, 1);
add_action('wpml_pro_translation_completed', 'publisher_save_post', 1000, 1)

Both hooks pass $post_id. From testing I found wpml_after_save_post fires when content is manually edited using the WordPress post editor. wpml_pro_translation_completed fires when using the Translation Editor. Only one or the other hook should fire based on the action performed.

I could not find a hook that fires when content is copied into a translation from the original post. Instead, I was able to use the above hooks and then find the associated translations using the following code:

    global $wpml_post_translations;
    if ($wpml_post_translations && is_post_type_translated( $post->post_type )) {
        $translated_ids = $wpml_post_translations->get_element_translations( $post_id, false, true );
        foreach ( $translated_ids as $lang_code => $translated_id ) {
            $translated_post = get_post($translated_id);
            if ( $translated_post ) {
                // Sync here;
            }
        }
    }

At the point the above hooks fire, all content has already been copied to the translations.

The topic ‘[Closed] Hook firing after a translation is created/updated?’ is closed to new replies.