Skip Navigation

[Resolved] Auto duplicate frontend submission forms posts to all languages

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

Problem:

The issue here is that the user want their post to duplicate to their other languages when a form is submitted.

Solution:

This can be done by using the code below.


function quick_duplicate_save_data_action( $post_id, $form_data ){
$forms = array( 2283, 2303, 2304 );
if ( in_array($form_data['id'],$forms) ){
    global $sitepress;
    do_action( 'wpml_make_post_duplicates', $post_id);
  }
}
add_action( 'cred_save_data', 'quick_duplicate_save_data_action', 10, 2 );

All that you need to do is change the ids in the array to match the id of the forms that you want this code to affect.

[edit] Note that the above will create WPML duplicates, which means the posts are synched and the "translations" cannot be independently edited except in the back-end when converting from duplicates to translations. The code above can be updated to do this automatically when the "duplicates" are first created. See https://toolset.com/forums/topic/edit-independently-in-front-end-ro-en-title-content-of-duplicated-posts/#post-1239002
[/edit]

This support ticket is created 5 years, 9 months 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 15 replies, has 3 voices.

Last updated by valentinP-3 5 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#1200117

I am trying to: Auto duplicate frontend submission forms posts to all languages

Link to a page where the issue can be seen: bra.ro

I tried using this support tickets:
https://wpml.org/forums/topic/duplicate-translation-for-specific-language-on-the-frontend-with-cred-form/
https://toolset.com/forums/topic/duplicate-translation-for-specific-language-with-cred-form/#post-1147391

#1200205

Hello, do you have a specific question about this? Is it not working for you?

#1200377

Unfortunately it does not work.

#1200468

We would like to autoduplicate the content submitted in front end, in the other two languages we have. And it does not work. Is there a solution WPML-Toolset? we tried the above mentioned code, but not working. thx

#1200543

1. What is the primary language of your site?
2. What are the secondary languages of your site?
3. Is the Form on a primary language page, or a secondary language page?
4. Please share the code you have used so I can see the changes you made.
5. Is the original post created as expected, or is there an error?

#1200567

1. Primary = Romanian

2. Secondary = English + German

3. The form is in english, but it is also translated (duplicated) into both languages.

4. 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 );

function quick_duplicate_save_data_action( $post_id, $form_data ){
$form_langs = array(
729 => 'en',
1177 => 'de',
1178 => 'ro',
);
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 );

5. The original post is created as expected (ok). When introducing a new post in Romanian language, sometimes it's duplicated well but only in english (not in german), but not all the time. Sometimes the post does not duplicate but it creates wrong links that point to other translations.

#1200664

The code above is for a very specific case where the User does not want to duplicate the post into all the secondary languages right away. Your code will be simpler. Try this instead:

function quick_duplicate_save_data_action( $post_id, $form_data ){
    $forms = array( 123, 456, 789 );
    if ( in_array($forms, $form_data['id'] ) ){
        global $sitepress;
        // Use new WPML API   
        do_action( 'wpml_admin_make_post_duplicates', $post_id ) ;  
    }      
}    
add_action( 'cred_save_data', 'quick_duplicate_save_data_action', 10, 2 );

Change 123, 456, 789 to be a comma-separate list of your Form IDs. The order of the numbers is not important.

#1200877

Hi Cristian, still does not work, below is our functions.php file in the child theme. In Romanian and English it simply save the post but no duplication.
Moreover, in german language - add new post, it save the post so I can see it in german posts but the post link is wrong - it goes to other post in english
One other question that may be important: Is it mandatory to have the Multilingual Tools plugin installed, are there any other specific settings we need to make there?
Please advise, thx

<?php
/**
* @package WordPress
* @subpackage Kleo
* @author SeventhQueen <themesupport@seventhqueen.com>
* @since Kleo 1.0
*/

/**
* Kleo Child Theme Functions
* Add custom code below
*/

add_shortcode('trim', 'trim_shortcode');
function trim_shortcode($atts, $content = '') {
$content = wpv_do_shortcode($content);
$length = (int)$atts['length'];
if (strlen($content) > $length) {
$content = substr($content, 0, $length) . '…';
}
return $content;
}

/**
* Auto-set post title
*/
add_action( 'cred_save_data', 'tssupp_form_submit', 10, 2 );
function tssupp_form_submit( $post_id, $form_data ){

if ( in_array( $form_data['id'], array( 729, 1177, 1178 ) ) ) {

$first = get_post_meta( $post_id, 'wpcf-contact-name', true );
$last = get_post_meta( $post_id, 'wpcf-surname', true );

wp_update_post( array(
'ID' => $post_id,
'post_title' => $first . ' ' . $last
)
);
}
}

function quick_duplicate_save_data_action( $post_id, $form_data ){
$forms = array( 729, 1177, 1178 );
if ( in_array($forms, $form_data['id'] ) ){
global $sitepress;
// Use new WPML API
do_action( 'wpml_admin_make_post_duplicates', $post_id ) ;
}
}
add_action( 'cred_save_data', 'quick_duplicate_save_data_action', 10, 2 );

#1201870

Hello,

Do you have a solution to this request?

Thx!

#1202493

Shane
Supporter

Languages: English (English )

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

Hi Valentin,

I must apologize for the delay in response as Christian is currently on Vacation so I will be handling the ticket here.

Would you mind providing me with access to the site so that I can run some debugging on this to check to see why it is not working for you ?

The private fields have been enabled for your next response.

Thanks,
Shane

#1202955

Ok. Unfortunately, I can't see the private fields where I can insert my login settings.

#1203204

Shane
Supporter

Languages: English (English )

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

Hi Valentin,

Please check now, they should be enabled.

Thanks,
Shane

#1203440

Shane
Supporter

Languages: English (English )

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

Hi Valentine,

I'm trying to improve on the code for you, however I need to know which form will the user always be using to fill out the information as well as what are the other form translations.

Currently the code is this.

function quick_duplicate_save_data_action( $post_id, $form_data ){
$forms = array( 729, 1177, 1178 );
if ( in_array($forms, $form_data['id'] ) ){
 $form_langs = array(
    123 => 'en',
    456 => 'de',
    789 => 'ro',
  );
  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 );

But i need to replace the id with the correct id's of the form language i.e en must map to the id of the en for etc.

Please let me know.
Thanks,
Shane

#1203553

Hi Shane, the ID's are as follows:
2283 English - then the other two (2303 German and 2304 Romanian) are duplicated forms of the English one.
Is it mandatory that the user will introduce the data through the first we designed, the English one? (as said the other one are duplicated), or is it possible to introduce posts through all of them? Maybe if we translate the forms independently?
We would prefer to introduce the data in the Romanian one, but not be limited to this option.
Thx
Valentin

#1204579

Shane
Supporter

Languages: English (English )

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

Hi Valentin,

I've managed to fix the code and it should now be working.

The final code is.

function quick_duplicate_save_data_action( $post_id, $form_data ){
$forms = array( 2283, 2303, 2304 );
if ( in_array($form_data['id'],$forms) ){
    global $sitepress;
    do_action( 'wpml_make_post_duplicates', $post_id);
  }
}
add_action( 'cred_save_data', 'quick_duplicate_save_data_action', 10, 2 );

Please let me know if this helps.
Thanks,
Shane