Skip Navigation

[Resolved] Duplicating Post to all other Secondary languages with Cred form and WPML

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

Problem:

I have a form to submit a new Recipe in default language (arabic). now after submitting the form with the help of following code snippet i can confirm that on the frontend i can find the post in default language (arabic) and when i switch the language to english i can see also a post of default language has been created for english.

https://toolset.com/forums/topic/duplicating-post-to-all-other-secondary-languages-with-cred-form-and-wpml/#post-1144446

the problem is when i edit the duplicated post that was created for english. I translate the fields to english and then submit the editing form, once the editing form is submitted, the default language disappears (no arabic version anymore, only english)

Solution:

You might consider to remove the a custom hidden field "_icl_lang_duplicate_of" from secindary language posts, for example:

https://toolset.com/forums/topic/duplicating-post-to-all-other-secondary-languages-with-cred-form-and-wpml/#post-1144834

Relevant Documentation:

This support ticket is created 6 years, 2 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.

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

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 12 replies, has 2 voices.

Last updated by Nashaat 6 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#1144446

I am trying to follow this Ticket here: https://toolset.com/forums/topic/cred-custom-post-type-front-end-translations/

FRONTEND:

I have a form to submit a new Recipe in default language (arabic). now after submitting the form with the help of following code snippet i can confirm that on the frontend i can find the post in default language (arabic) and when i switch the language to english i can see also a post of default language has been created for english.

function quick_duplicate_save_data_action( $post_id, $form_data ){
    
        // Change your CRED Form "ID" accordingly below
        if ($form_data['id']==75){
  
            // 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 );

the problem is when i edit the duplicated post that was created for english. I translate the fields to english and then submit the editing form, once the editing form is submitted, the default language disappears (no arabic version anymore, only english)

I have added also following shortcode to see what language i am editing, actually it shows me the right language when editing (ar/en)


function wpml_language_code_value_func() {
    $return = ICL_LANGUAGE_CODE;
    return $return;
    }   
add_shortcode('wpml_language_code_value', 'wpml_language_code_value_func');


BACKEND:
in the backend after submitting the Recipe with the default language, it doesn't really duplicate the post into other language. when looking at the item in Recipes i can see that the plus Icon still showing up to add translation for english.

I think its working half way for now, i am not sure what need to be modified that everything work from the frontend.

#1144448

update:

I have deactivated all above mentioned code Snippets and tried the same process.

actually after submitting the form, on the front end i still can see the submitted post in both languages ar/en although the code snippets are deactivated. this means its just a normal behaviour when switching i can see the same post but it has only one language. nothing get really duplicated..

#1144452

The problem was coming from the Page of the cred form. I have translated this page to english.. after deleting the english translation looks like this solved the issue.

so this means when posting a new Recipe the page where the cred form cant be translated to other language? it must always be in arabic? its not possible to post in arabic (default language) but let the user chose which language has the form?

one more thing about duplicating the post. it works fine but one thing is missing which is the Featured image. after submitting the form in the default language(arabic) the created copy for english appear but without featured image. when i edit the english copy the chose image field has no image assigned to it. is there away to solve this? i found a manual solution for it in WPML -> Settings-> Media Translation -> Start Button..
This will relink the image back. i wonder if there is another solution..

Thanks

#1144463
#1144830

The only Issue i have now is following: if i update the recipe with the default language.. The english duplicate get overwritten with the default language.

Now from the backend if i update the english copy, there is a wpml option "Translate independently", this helps in this situation!

So how can i duplicate to secindary languages automatically when submitting new Recipe in default language and let this new copies of get translated independently. this could solve the overwrite issue.

#1144834

You might consider to remove the a custom hidden field "_icl_lang_duplicate_of" from secindary language posts, for example:

function quick_duplicate_save_data_action( $post_id, $form_data ){
        // Change your CRED Form "ID" accordingly below
        if ($form_data['id']==75){
            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 );
#1144846

MANY THANKS Luo.
One last question and i will close this topic.

i am getting the arabic slug name also copied to the english copy for example

arabic slug: hidden link
english slug: hidden link

After editing the english copy arabic slug remains.
so how can i get rid of the title in arabic and assign english to the copy from title?

#1144862

I suggest you edit the post slug name manually, for example:
edit the english post: hidden link

click "Screen Options", enable option "Slug", then you will be able to see a section "Slug", there you can setup the post slug manually.

#1144888

This is possible for sure from the backend. but i am looking to do this with cred form fromm the frontend, is there a slug field for cred form?

#1144891

No, there isn't such a built-in feature within Toolset form plugin, if you agree, we can take it as a feature request, our developers will evaluate it.

#1144892

This has automate the slug name from title. if you have a better way i would be happy

add_action('cred_save_data', 'post_title_commentary', 10, 2);
function post_title_commentary($post_id, $form_data) {
  
	if ( ($form_data['id'] == 113) ){
 
	$field1 = get_post_meta($post_id, 'post_title', true);
   

    $post_slug = $field1; 
	$slug = sanitize_title($post_slug); 
  
	wp_update_post(array('ID'=>$post_id,'post_name' => $slug));
	}
}
#1144897

When you duplicate the post into other language versions, the post title of other language versions is still same as your original language post, for example "the-title-in-arabic". So you have to manually change it manually in front-end or admin side.

But in font-end, you need to setup another post form for editing the other language post, use the custom PHP codes to change the post slug, same as your post:
https://toolset.com/forums/topic/duplicating-post-to-all-other-secondary-languages-with-cred-form-and-wpml/#post-1144892

I think that is the best workaround in your case.

#1148560

Thank you for your great support Luo!