Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client is trying to trigger an action when a custom post type translation is saved using the WordPress hook. The problem is with the is_admin function.

add_action('save_post_ebc_business_card', [$this, 'generate_pkpass_on_save'], 10, 3);

. This hook works as expected for the original language but does not trigger when saving or updating a translated version of the post.
Solution:
We recommend modifying the condition to check if the request originated from the WPML Advanced Translation Editor (ATE) by using the following code:

$is_wpml_translation = false;<br />if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'ate.wpml.org') !== false) {<br />    $is_wpml_translation = true;<br />}<br />if (is_admin() || $is_wpml_translation) {<br />    $this->initialize_admin();<br />}

This code snippet checks the HTTP_REFERER to determine if the request came from the WPML ATE and adjusts the execution environment accordingly.

If this solution does not resolve your issue or seems irrelevant due to being outdated or not applicable to your specific case, we highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If further assistance is needed, please open a new support ticket at WPML support forum.

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.

Our next available supporter will start replying to tickets in about 2.64 hours from now. Thank you for your understanding.

This topic contains 14 replies, has 3 voices.

Last updated by Itamar 1 month ago.

Assisted by: Itamar.

Author Posts
October 5, 2024 at 12:13 pm #16256494

mohammeda-24

Background of the issue:
I’m trying to trigger an action when a custom post type translation is saved. Currently, I’m using the following WordPress hook: add_action('save_post_ebc_business_card', [$this, 'generate_pkpass_on_save'], 10, 3);
This hook works as expected when saving the post in the original language.

Symptoms:
The hook doesn’t seem to trigger when saving or updating a translated version of the post.

Questions:
Is there an alternative hook or function I can use to ensure the generate_pkpass_on_save function is called when the translation of the custom post type is saved?

October 6, 2024 at 6:04 pm #16258335

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+02:00)

Hi,

We have a wide range of hooks for WPML. Please check the following link to see if you can find the hook that fits your needs.

https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

If you need further help, please describe exactly what you are trying to achieve when saving a post.

Regards,
Itamar.

October 7, 2024 at 11:06 am #16260816

mohammeda-24

Hello,

I previously reviewed those hooks, but unfortunately, none of them matched the save_post_[custom-post-type] hook specifically.

I am currently generating a .pkpass file for iOS Wallet when posts are saved or published. This file is correctly generated for the original language version of the post. However, I also need the file to be generated whenever a translation is completed or updated. I’m using the Advanced Translation Editor for this.

Thank you!

October 8, 2024 at 8:14 am #16264281

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+02:00)

Hi, and thanks for the extra information about your case.

I'm consulting our second-tier supporters about this matter. I'll update you here once I have their reply.

I appreciate your patience.
Itamar.

October 9, 2024 at 9:00 am #16268762

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+02:00)

Hi,

Our second-tier supporter checked this issue, and according to his test, the standard WordPress save_post hooks should be possible to use when completing a translation. This is what our second-tier supporter explained.

I did some testing and can confirm that it should be possible to use the standard WordPress save_post hooks when completing a translation.

Our code that creates the translated post goes through the method insert_post in the file plugins/sitepress-multilingual-cms/inc/post-translation/wpml-create-post-helper.class.php.

There—depending on whether this is a new translation or an update—we either use the standard WordPress function wp_insert_post or wp_update_post:

		if ( isset( $postarr['ID'] ) ) {
			/**
			 * Prevents from bug on multiple update post calls during one request.
			 * During first update post call wrong terms in the original language were put to the cache in the process of post revision save.
			 *
			 * @see <em><u>hidden link</u></em>
			 */
			$returnTrue = \WPML\FP\Fns::always( true );
			add_filter( 'wpml_disable_term_adjust_id', $returnTrue );
			$new_post_id = wp_update_post( $postarr, $wp_error );
			remove_filter( 'wpml_disable_term_adjust_id', $returnTrue );
		} else {
			add_filter( 'wp_insert_post_empty_content', array( $this, 'allow_empty_post' ), 10, 0 );
			$new_post_id = wp_insert_post( $postarr, $wp_error );
			remove_filter( 'wp_insert_post_empty_content', array( $this, 'allow_empty_post' ) );

In either case that means triggering the standard WordPress hooks.

In my case I simply checked if they were triggered (when translating a standard Post) with

add_action( 'save_post_post', function(){

    error_log("save_post_post triggered");
});

add_action( 'save_post', function(){

    error_log("save_post triggered");
});

and can confirm that they were in both cases.

So we can't know why it is not working for you. If you'd like we can check this issue further. For we want to ask you to replicate this issue on a sandbox site. I've created one for you and you can access it here.

hidden link

If you can replicate the issue on the test site, please explain to us in details the steps you took and the code you added.

Regards,
Itamar.

October 9, 2024 at 9:40 am #16268998

mohammeda-24

Hello,

Thank you.

I installed my plugin and added the debugging code to the theme functions.php

I made couple of tests for the custom post type:

1- Original language ( debugging triggered)
2- Translation ( Debugging not triggered )

I also made couple of tests with the wordpress posts:

Both languages debugging triggered

it seems that the issue is related to the custom post type only.

October 9, 2024 at 7:43 pm #16272254

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+02:00)

Hi,

Debugging your custom plugin is out of the scope of our support forum. I hope you can understand this. The fact that it works with WordPress posts tells us that there is no problem from our side. We have extensive documentation for developers. The main page is here.

https://wpml.org/documentation/support/

More specifically, you can check this page.

https://wpml.org/documentation/support/achieving-wpml-compatibility-for-your-themes-and-plugins/

We can try to help further only if you point us to the code from which you think the problem stems. Then, our second-tier supporter can take a second look.

Regards,
Itamar.

October 10, 2024 at 9:28 am #16273785

mohammeda-24

Hello,

Thank you for your assistance.

You are correct that both actions 'save_post' and 'save_post_[post_type]' are indeed triggered when updating or saving translations. Upon further investigation, I discovered that my action was conditioned on "is_admin()". Since the WPML Translation Editor is not recognized as an admin page, this condition prevented the action from being triggered as expected.

I’m considering alternatives to ensure the action triggers within the Translation Editor. If there are any specific conditions that align with the WPML Translation Editor, similar to is_admin(), I would appreciate any guidance on that.

Thank you once again for your help!

October 10, 2024 at 1:12 pm #16275286

Carlos Rojas
Supporter

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

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

Hello,
My name is Carlos and I will continue working on this ticket while my colleague Itamar is on vacation.

I asked our 2nd tier specialists if there is a similar function to is_admin() within our code. I will get back to you as soon as I get an answer.

Thank you very much for your patience and understanding!

October 11, 2024 at 11:24 am #16279203

Carlos Rojas
Supporter

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

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

Hi there,

Here is the answer from our 2nd tier specialists:

..note that the global $_POST request will include an entry for trid, which comes from WPML, and if that is not specific enough they could check the referrer data available in the $_SERVER global.

I hope this helps you improve the code 🙂

October 13, 2024 at 4:46 pm #16283336

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+02:00)

Hi,

Please let me know if you have any further questions.

Regards,
Itamar.

October 14, 2024 at 1:36 pm #16286328

mohammeda-24

Hello,

Thank you for all the help and suggestions. Based on the advice provided, I added the following code to detect when a translation is saved through the WPML Advanced Translation Editor (ATE):

$is_wpml_translation = false;

// Check if the request originated from the WPML ATE
if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'ate.wpml.org') !== false) {
$is_wpml_translation = true;
}

// Execute admin logic if in the admin area or if WPML translation editor
if (is_admin() || $is_wpml_translation) {
$this->initialize_admin();
}

Thanks again for the helpful guidance!

October 14, 2024 at 8:12 pm #16287788

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+02:00)

Hi,

Great, I'm glad we could help!

Thanks for updating us with your code.

Best Regards,
Itamar.

October 15, 2024 at 8:04 am #16289097

mohammeda-24

Hello,

I believe the problem description could be clarified a bit. The save_post action actually does work with WPML translation editor. In my specific case, the issue arose because I was using the is_admin() condition, which doesn't trigger when working within the WPML Translation Editor.

I believe the problem description should target the primary issue which was the use of is_admin().

Thank you

October 15, 2024 at 11:40 am #16290344

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+02:00)

Hi,

Thanks for pointing this out.
I appreciate it!
I'll correct this.

All the best,
Itamar.