[Resolved] Add action when translation saved/updated
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.
. 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:
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.
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?
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.
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
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.
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.
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.
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.
..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.
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();
}
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().
Thanks for pointing this out.
I appreciate it!
I'll correct this.
All the best,
Itamar.
Manage Cookie Consent
We use cookies to optimize our website and services. Your consent allows us to process data such as browsing behavior. Not consenting may affect some features.
Functional
Always active
Required for our website to operate and communicate correctly.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
We use these to analyze the statistics of our site. Collected information is completely anonymous.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
These cookies track your browsing to provide ads relevant to you.