Skip Navigation

[Resolved] Duplicate Translation for specific language with Cred form.

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

Problem: I would like to use Forms to allow Users to automatically create a duplicate post in a secondary language after creating a post in the primary language with Forms.

Solution:
Create one edit post Form for each language. Remove everything except the submit button and the form messages field. Change the text on the submit button to something like "Create translation in Russian". Place these Forms in the template for the default language single post.

Add this WPML hook to create a single language duplicate from the default language post:

function wpml_make_post_duplicate_language( $master_post_id, $lang_to ) {
  global $sitepress;
  $master_post = get_post( $master_post_id );
  if ( 'auto-draft' === $master_post->post_status || 'revision' === $master_post->post_type ) {
      return;
  }
  $active_langs = $sitepress->get_active_languages();
  if (array_key_exists($lang_to, $active_langs)) {
    $trid      = $sitepress->get_element_trid( $master_post->ID, 'post_' . $master_post->post_type );
    $lang_from = $sitepress->get_source_language_by_trid( $trid );
    if ( $lang_from == $lang_to ) {
        return;
    }
    $sitepress->make_duplicate( $master_post_id, $lang_to );
  }
}
add_action( 'wpml_make_post_duplicate_lang', 'wpml_make_post_duplicate_language', 10, 2 );

Add this Forms API hook to trigger the correct WPML hook when the Form is submitted:

function quick_duplicate_save_data_action( $post_id, $form_data ){
  $form_langs = array(
  383 => 'en',
  404 => 'ru',
);
  if( isset( $form_langs[$form_data['id']] ) ) {
    global $sitepress;
    $lang_to = $form_langs[$form_data['id']];
    do_action( 'wpml_make_post_duplicate_lang', $post_id, $lang_to );
  }
}
add_action( 'cred_save_data', 'quick_duplicate_save_data_action', 10, 2 );

Modify the $form_langs array to support any number of secondary languages. The keys in the array should be the same as the language Form IDs. The values should be the corresponding two-letter language codes.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://wpml.org/forums/topic/duplicate-translation-for-specific-language-on-the-frontend-with-cred-form/#post-2908154

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 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: America/New_York (GMT-04:00)

This topic contains 9 replies, has 3 voices.

Last updated by Nashaat 6 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1147220

I am trying to duplicate translation for a specific language!

wishing to achieve following workflow from frontend:

1- user submit new post in default language (arabic).
2- user check if default language informations are right.
3- user has embed buttons of a cred forms in the post that creates a secondary language needed, each button creates only the chosen button of secondary language.

till now i have this workflow which works good but not ideal:

1- user submit new post in default language.
2- user check if default language informations are right.
3- user has ONLY one button that creates all secondary languages at once. why its bad? because if i add more secondary languages in the future to the site like german, then clicking on that button will override all translations and recreate them based on the default language which is bad.

for the workflow that i have now i have did following:

1- created a cred form for submitting posts in default language (arabic)
2- after submitting the post in default language the user see the post. in the post i have embed a cred form button that duplicate translations
3- i have added following hook for this button of the cred form.

function quick_duplicate_save_data_action( $post_id, $form_data ){
        // Change your CRED Form "ID" accordingly below
        if ($form_data['id']==192){
            global $sitepress;
            // Use new WPML API   
            do_action( 'wpml_admin_make_post_duplicates', $post_id ) ;  
             
            $trid         = $sitepress->get_element_trid( $post_id, 'post_' . $post->post_type );
            $translations = $sitepress->get_element_translations( $trid, 'post_' . $post->post_type );
              
            foreach ( $translations as $tr ) {
                if ( $tr->element_id != $post_id ) {
                    delete_post_meta( $tr->element_id, '_icl_lang_duplicate_of' );
                }
            }
        }      
}    
add_action( 'cred_save_data', 'quick_duplicate_save_data_action', 10, 2 );

4- user clicked on this button. all translation for all secondary languages get created at once, and the user can edit each of them

now my concern is to have a cred form for each language. this means in the post itself the user will see multiple button of differnet cred forms. each button create translation for specific language (e.g. "create EN translation" or "create RU translation")

is that possible to modify the hook so that only a specific language get created? for example 3 hooks for 3 languages and for 3 Forms that create translation independently.

#1147391

I can show you how to implement different hooks for different Forms. I can see for WPML ticket, Shekhar has provided an answer: https://wpml.org/forums/topic/duplicate-translation-for-specific-language-on-the-frontend-with-cred-form/#post-2908154
Add that code to your functions.php file. Then you can integrate that with Forms. Create 3 Forms, one for each language. You will need to know the 3 Form IDs. Then add the CRED hook like this:

function quick_duplicate_save_data_action( $post_id, $form_data ){
  $form_langs = array(
    123 => 'en',
    456 => 'de',
    789 => 'fr',
  );
  if( isset( $form_langs[$form_data['id']] ) ) {
    global $sitepress;
    $lang_to = $form_langs[$form_data['id']];
    do_action( 'wpml_make_post_duplicate_lang', $post_id, $lang_to );
  }
}
add_action( 'cred_save_data', 'quick_duplicate_save_data_action', 10, 2 );

* edit - updated to the correct language codes *

Change the $form_langs information to use the Form ID and corresponding language code for each Form.

#1148034
3-buttons-of-languages-addedto-single-recipe-layout.png
2.2-English-form-created.png
2.1-Russian-form-created.png
1-Function-added.png

Thanks Christian.

I have done steps in images attached but seems smething is missing:

1- I have added following code provided by Shekhar : https://wpml.org/forums/topic/duplicate-translation-for-specific-language-on-the-frontend-with-cred-form/#post-2913205. And then i have added your code and modified the id number of the forms as well the language

2.1- i have created the form for russian
2.2- i have created the form for english

3- I have embed the forms in my single Recipe Layout

now if i try to submit the buttons nothing happens the page refresh but no translation created. Also not in the backend.
i was thinking maybe its a conflict because i am using two forms in the same page so i tried to keep only the "create EN" button and tested but the same nothing happened.

here is link of a screen recording i have made : hidden link

#1149295

Try replacing the form_messages field in both Forms first. If there is a backend error or notification triggered, it will be displayed in the form_messages field. Without the field, it may not be obvious there was a problem.

[cred_field field="form_messages" class="alert alert-warning"]

Please test that and then if it's still not working as expected I can take a closer look.

#1150117

Hi Christian, It didnt work i tried to add the warning message in cred Form as well WP_DEBUG but it doesn't show me any Errors. I have tried to submit the form with AJAX but also no Errors.

#1150615

Okay please provide login credentials in the private reply fields here. I will create a clone of the site and run some additional tests.

#1150819

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nashaat,

As christian is off on holiday, I will be handling this ticket for you.

Could you let me know where you added the function that christian sent ?

Thanks,
Shane

#1150863

Hi shane,

You can find the both code snippets in here:
1- Toolset> Settings> Custom code > 11-wpml-duplicate-secondary-languages
2- Toolset> Settings> Custom code > 12-duplicate-translation-different-languages

please see attached files in this comment for better orientations : https://toolset.com/forums/topic/duplicate-translation-for-specific-language-with-cred-form/#post-1148034

#1153174

It turns out the language codes I used were inaccurate. Please change the $form_langs array in your cred_save_data hook as shown here:

  $form_langs = array(
    383 => 'en',
    404 => 'ru',
  );

Then test again and let me know if translations are not created. I tested the English and Russian buttons on my local clone and translations were created after submission.

#1153321

My issue is resolved now. Thank you!