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 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 6 years, 1 month ago.
Assisted by: Christian Cox.