Skip Navigation

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.

This topic contains 22 replies, has 3 voices.

Last updated by sarah.n 5 years, 9 months ago.

Assigned support staff: sarah.n.

Author Posts
December 9, 2014 at 7:14 am #525195

Timothy

Hi Andreas,

#1. I just checked the FTP and changed the password back to the one in which I have sent. So you should be able to login now

#2. Yes I will attach the image. I have already set that.
I have even tried to bypass the loop and just run the code:

$iclTranslationManagement->make_duplicates_all( $post_id );

It goes into the if loop but just not doing the make_duplicates_all.

December 9, 2014 at 8:18 am #525237

Andreas Neuber
Supporter

Languages: English (English ) German (Deutsch )

# To get this resolved and give you really working sample code, I was trying this:
- creating a fresh WP installation with WPML plugins
- adding a simple page template with a simple post form
- I added a "wp_insert_post" function into this page template
- in "functions.php" of the TwentyFourteen theme I added the code I gave you before
(also this one -> https://wpml.org/forums/topic/how-to-automatically-create-duplicate-translations/#post-507498 )

# Result:
The very same problems you have :-/
The "master post" is inserted - wp_insert_post()
However the duplicates in the other languages are not created.

At first I had an error regarding a missing class object - I added

if ( !isset( $iclTranslationManagement ) ) {
      require ICL_PLUGIN_PATH . '/inc/translation-management/translation-management.class.php';
      $iclTranslationManagement = new TranslationManagement;
}

The eliminated this error, but still no duplication took place.

# I'm very sorry that I'm stuck at the moment - I will bring this now to the attention of our developers.
http://wpml.org/documentation/support/wpml-coding-api/ gives a good example with a CRED frontend form but many WPML users try to use "icl_makes_duplicates()" with their own frontend forms.

December 9, 2014 at 9:01 am #525271

Timothy

Hi Andreas,

Thank you very much. Finally seems like we are on the same page 😛

Really hope there is a way where we can solve this. Looking forward for some good news. Thanks!

December 10, 2014 at 3:38 pm #526366

Timothy

Is there any solution? Or is there anything I can do instead of using the function make_duplicates_all? Could I use

sql

query to create the database fields?

December 11, 2014 at 11:03 am #526861

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hello Timothy,

This is Sarah here. Andreas forwarded your forum thread to me so I will do my best to assist you further.

First please accept my apologies for the delay!

Now I understand you are using wp_insert_post() and you would like to duplicate the post being inserted, correct?

I just tested the following basic example and it works well for me. You will need to adjust it of course to suit your needs but here we go:


add_action( 'save_post', 'wpml_duplicate_on_publish', 200, 1);
function wpml_duplicate_on_publish( $post_id ) {
    if(function_exists('icl_makes_duplicates')){

		// we need this to avoid recursion see add_action at the end
		remove_action('save_post', 'wpml_duplicate_on_publish', 200 );

		global $sitepress;
		if (is_object($sitepress)) {
			$sitepress->icl_translations_cache->clear();
		}

		icl_makes_duplicates_public( $post_id );
		
		// must hook again - see remove_action further up
		add_action('save_post', 'wpml_duplicate_on_publish', 200, 1 );
	}
}

function my_footer_hook() {
		$new_post = array(
 		'post_content'   => 'Test',
  		'post_name'      => 'insert-test',
 		'post_title'     => 'Insert Test',
 		'post_status'    => 'publish',
 		'post_type'      => 'post',
 		'post_category'  => array(1)
		); 

		wp_insert_post($new_post);

}
add_action('wp_footer', 'my_footer_hook');

You see I am running wp_insert_post() on my theme's wp_footer. So to test the example above you only need to add the code in your theme's functions.php and call your site's front end once.

Refresh your admin posts screen and you should see the newly inserted post and its duplicates.

Hope this helps.

December 11, 2014 at 1:27 pm #527019

Timothy

Hi Sarah,

If you check on the previous messages I have with Andreas, we both were able to get regular posts to duplicate. However, it is when we want to duplicate custom posts "listings" we were unable to do so.

I have created a form for users to create a custom post in the front end and the goal is to create the custom posts into different languages.

I have seen this solution on other WPML forum as solution:

function duplicate_master_post($post_id, $post, $update){
    global $sitepress, $iclTranslationManagement;
     
     
    $only_cpt   = array();
    $only_cpt[] = 'listings';
     
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    // don't save for revisions
    if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
        return;
    }
    // only duplicate when publishing
    if ($post->post_status != 'publish') {
        return;
    }
    // avoid recursion
    remove_action('wp_insert_post', 'duplicate_master_post', 10);
 
    if ( !isset( $iclTranslationManagement ) ) {
        $iclTranslationManagement = new TranslationManagement;
    }
        
    $post_type = get_post_type( $post_id );
    if ( $sitepress->is_translated_post_type( $post_type ) && in_array( $post_type, $only_cpt ) ) {
        $iclTranslationManagement->make_duplicates_all( $post_id );
    }
}
add_action('wp_insert_post', 'duplicate_master_post', 10, 3);

But if you read the results I have encountered on previous posts with Andreas I am getting stuck on the make_duplicates_all functions.

December 11, 2014 at 1:46 pm #527034

Timothy

OMG I actually tried that and now it can duplicate my custom posts as well. Thank you very much this works perfectly. Thanks!

December 11, 2014 at 2:01 pm #527046

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

So good to hear 🙂 You are welcome!