Please make sure to update to WPML 4.3.6 and check our list of Known Issues before reporting

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.

Our next available supporter will start replying to tickets in about 5.32 hours from now. Thank you for your understanding.

This topic contains 22 replies, has 3 voices.

Last updated by sarah.n 5 years, 1 month ago.

Assigned support staff: sarah.n.

Author Posts
December 4, 2014 at 2:31 pm #523168

Timothy

I am trying to: I am trying to have users to fill in a form to create a new posting. Then this post will be duplicated to different languages I have.

I visited this URL:

I expected to see: Afterwards I expected that the post will be duplicated however, I get a blank page afterwards and seems like the make_duplicate class is not found. This was working before but unfortunately it does not work anymore.

Instead, I got: Blank screen and if i turn debug testing on I see the following:
Fatal error: Call to undefined method stdClass::make_duplicate() in /var/www/vhosts/domain.com/httpdocs/wp-content/themes/theme_name/functions.php on line 3768

December 4, 2014 at 3:41 pm #523234

Timothy

By the way I have declared the lib with the following:

global $iclTranslationManagement, $sitepress, $ICL_Pro_Translation;
	if ( !isset( $iclTranslationManagement ) ) {
        if(!class_exists('TranslationManagement')){
            include(ABSPATH.'wp-content/plugins/sitepress-multilingual-cms/inc/translation-management/translation-management.class.php');
            include(ABSPATH.'wp-content/plugins/sitepress-multilingual-cms/inc/translation-management/pro-translation.class.php');
        }
        $iclTranslationManagement = new TranslationManagement;
        $ICL_Pro_Translation      = new ICL_Pro_Translation();
    }
December 5, 2014 at 7:56 am #523564

Andreas Neuber
Supporter

Languages: English (English ) German (Deutsch )

Sorry for the "undefined" trouble!

I remember this problem from some other similar tickets. Looks as if we should update our documentation a bit. Will talk to our team about it.

In order to find a solution for your challenge, could you please try this as test?

1) Copy this code http://pastebin.com/bVNkYQXW into your themes "functions.php" file

2) Create a new post under "Posts > Add new"

3) Now, please check - was this post duplicated in all your languages?

If so, very good, then we need to adjust this further to fit your user form.

December 5, 2014 at 10:44 am #523702

Timothy

Yes this worked actually.

Do you know how what I can change to fix the custom post type and duplicate the posts?

December 5, 2014 at 10:58 am #523721

Timothy

In addition, this is for a custom post type I have created:

function create_post_type() {
  register_post_type( 'listings',
    array(
      'labels' => array(
        'name' => __( 'Listings' ),
        'singular_name' => __( 'Listing' )
      ),
    'public' => true,
    'has_archive' => true,
	'rewrite' => array('slug' => 'listing'),
    )
  );
}
December 5, 2014 at 1:40 pm #523869

Andreas Neuber
Supporter

Languages: English (English ) German (Deutsch )

Happy to hear.
Here 1 more listing, very similar: http://pastebin.com/bZE820Ja

# Now when you look at line 26:

$sitepress->is_translated_post_type( $post_type )

This code checks if the post type is a "translatable" one.
The standard WP post is always translatable. But a new custom post type like your "listings" needs to be set to "Translate". This you can do under "WPML > Translation Management > Multilingual Content Setup > Custom Posts".

in_array( $post_type, $only_cpt )

This code makes sure that the duplication only happens with specific custom post types. So in line 5 you need to replace "book" with "listings".

# Could you adjust the code and run one more test?
This time again in the backend but use "Listings > Add new".
Again - is the new listing automatically duplicated in all languages?

December 5, 2014 at 5:15 pm #524000

Timothy

Hi Andreas,

I tried the following added the following to the function.php within theme.

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

Afterwards in one of the page I tried the following:

$posts_array = get_post( 595 );
duplicate_master_post(595, $posts_array, null);

However, when the page loads it breaks.

December 5, 2014 at 5:17 pm #524001

Timothy

It is still at the problem:

$iclTranslationManagement->make_duplicates_all( $post_id );
December 6, 2014 at 6:37 pm #524274

Timothy

Is there any solution for this or can I give you the access information to have a look at it. Seems like we going through loops with the problem of the class rather than getting a solution.

December 8, 2014 at 12:26 pm #524756

Andreas Neuber
Supporter

Languages: English (English ) German (Deutsch )

Sorry for the delay!

# Regarding https://wpml.org/forums/topic/make-duplicates-of-a-post-does-not-work/#post-524000

You should not need an extra:

$posts_array = get_post( 595 );
duplicate_master_post(595, $posts_array, null);

Line "add_action('wp_insert_post', 'duplicate_master_post', 10, 3);" ensure that the function "fires" when a post is created.

# Yes - credentials for your website would be good
I'm quite interested to get this going. We need some working code for this API function.
You should see a protected webform all they way down here in this thread. (it asks first for "Backup + Legal disclaimer")
Thanks in advance!

December 8, 2014 at 1:26 pm #524841

Timothy

Hi Andreas,

I am still unsure what you mean by add_action('wp_insert_post', 'duplicate_master_post', 10, 3);

I currently have a shortcode that would create a new listing:

$post = array(
						'post_content' => '',
						'post_title' => $_POST['pacadddress'],
						'post_status' => 'pending',
						'post_type' => 'listings'
					);
					$post_id = wp_insert_post( $post );

It then returns the new post_id in which it will create different language's translation.

Before it worked when I did the following but now it doesn't work now.

if($sl != 'en')
					{
						echo 'en';
						$new_lang_id[] = $iclTranslationManagement->make_duplicate($post_id, 'en');
					}
					if($sl != 'zh-hans')
					{
						echo 'zh-hans';
						$new_lang_id[] = $iclTranslationManagement->make_duplicate($post_id, 'zh-hans');
					}
					if($sl != 'zh-hant')
					{
						echo 'zh-hant';
						$new_lang_id[] = $iclTranslationManagement->make_duplicate($post_id, 'zh-hant');
					}

Do you have an email address where I can send you the login information for you to help me have a look as we are ready to alunch the website but the only thing that is not working is this problem.

December 8, 2014 at 5:15 pm #524987

Timothy

Hi Andreas,

I totally understand now the line:
add_action('wp_insert_post', 'duplicate_master_post', 10, 3);

when the custom posts creates it will fire the code.

but the problem is as follows:

echo 'same problem';
$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 );
}
echo ' here';

I have attached the screenshot of when I want to create the custom post:
I have set before it goes into the make_duplicates_all to echo "Same Problem" then afterwards to post here. So it never reached "Here"...

December 9, 2014 at 5:59 am #525181

Andreas Neuber
Supporter

Languages: English (English ) German (Deutsch )

Thanks - 2 questions:

1) Credentials
https://wpml.org/forums/topic/make-duplicates-of-a-post-does-not-work/#post-524756
I mentioned here clearly that there is a protected webform for you to enter credentials.
Was the form not visible? (hope we dont have JS problem there).

I displayed the form for you again - you should see it below my reply here.
Please use it, because thats really the only way to involve other supporters & developers as well.

2) Thanks for code snippet here: https://wpml.org/forums/topic/make-duplicates-of-a-post-does-not-work/#post-524987

What is the location of this file?
Is it the "functions.php" of your theme?

December 9, 2014 at 6:15 am #525183

Timothy

Thank you very much. It was really weird I did not see the web form until now. Anyways I have supplied the information and sent the webform. Really appreciate your help. Hoping can have this resolved.

December 9, 2014 at 7:04 am #525190

Andreas Neuber
Supporter

Languages: English (English ) German (Deutsch )

Thanks a lot!

# WP admin access works - FTP password not. Might be just a small typo.
Could you check this for me?

# Why did this part of the code not work?

echo 'same problem';
$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 );
}
echo ' here';

You see this condition

$sitepress->is_translated_post_type( $post_type )

Duplicates are only created of the "listings" post type is "translatable".

As mentioned further up you need to make your new "listings" translatable - manually.
This needs to be done under "WPML > Translation Management > Multilingual Content Setup > Custom Posts". Please set it to "Translate".