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

Hi, Amit here, I am the WPML Support Manager, our current ticket queue is high, update your WPML plugins and make sure you meet the minimal requirements for running WPML before reporting an issue please - many tickets are resolved doing that

Please look at our updated list of Known Issues and you can also use our support search to find helpful information and of course review our documentation before opening a ticket.

If you do need to open a ticket please make sure to provide us with all the needed information as described in this page

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 12 replies, has 3 voices.

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

Assigned support staff: sarah.n.

Author Posts
January 27, 2014 at 2:55 am #327943

jin woo

I have followed salar's code ( http://wpml.org/forums/topic/auto-duplicate-postspages-on-publish/#post-327939 )
and it seems to be working good on Back-end. but not my custom front-end form.

/**
* Duplicate a post/page/custom post on publish or update
*
**/
add_action('save_post', 'wpml_duplicate_on_publish');
function wpml_duplicate_on_publish ( $post_id ) {
global $sitepress, $iclTranslationManagement;

// don't save for autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// don't save for revisions
if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
return $post_id;
}
// Check permissions
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}

$master_post_id = $post_id;
$master_post = get_post($master_post_id);
$language_details_original = $sitepress->get_element_language_details($master_post_id, 'post_' . $master_post->post_type);

// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'wpml_duplicate_on_publish');

foreach($sitepress->get_active_languages() as $lang => $details){
if($lang != $language_details_original->language_code){
$iclTranslationManagement->make_duplicate($master_post_id, $lang);
}
}

// re-hook this function
add_action('save_post', 'wpml_duplicate_on_publish');
}

------------------------------

Fatal error: Call to a member function make_duplicate() on a non-object in /home/hosting_users/aaa/www/wp-content/themes/jobs/functions.php on line 1548

1548 $iclTranslationManagement->make_duplicate($master_post_id, $lang);

Am I missing something on my front-end? should I add any codes for fixing it?

and I just downloaded Version 3.0.2-a for fixing "Draft" when you duplicate posts.
it setup " Same as the original document". but it's still "draft" when I duplicate posts with this function. "This document is a duplicate of Auto Draft and it is maintained by WPML." this is what is written on a panel on right top of add new page.

Thanks,

January 27, 2014 at 10:20 am #328125

Dat Hoang

Hello jin woo,

I'm assigning this ticket to Sarah.
Please stay tuned and wait for the answer from her.

Thank you.

January 27, 2014 at 4:03 pm #328470

jin woo

thanks dat,

additionally, I need to know if I can setup post-status when it's posted.

like this... " if ($a=='publish') {...." then publish the posts. cuz we have little different options to allowed to be "published" or "pending".

Thanks for ur time again.

January 27, 2014 at 5:43 pm #328514

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hello jin woo,

This is Sarah here. Dat forwarded your forum thread to me so I will try to assist further.

I would suggest you remove that code you found and use this function instead:

icl_makes_duplicates($master_post_id);

The function will create duplicate posts from a master post ID to all the site’s languages. The master post doesn't need to be in the default language.

Why don't you give that a go and let me know how you get on?

January 29, 2014 at 6:28 am #329404

jin woo

Hello,

I have added your function on functions.php and added the new one you gave me. but it's not working.

 * Duplicate a post/page/custom post on publish or update
 * 
 **/
add_action('save_post', 'wpml_duplicate_on_publish');
function wpml_duplicate_on_publish ( $post_id ) {
    global $sitepress, $iclTranslationManagement;
     
    // don't save for autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    // don't save for revisions
    if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
        return $post_id;
    }
    // Check permissions
    if ( 'page' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) ) {
            return $post_id;
        }
    } else {
        if ( !current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
        }
    }
 
    $master_post_id = $post_id;
    $master_post = get_post($master_post_id);
    $language_details_original = $sitepress->get_element_language_details($master_post_id, 'post_' . $master_post->post_type);
     
    // unhook this function so it doesn't loop infinitely
    remove_action('save_post', 'wpml_duplicate_on_publish');
     
    foreach($sitepress->get_active_languages() as $lang => $details){
        if($lang != $language_details_original->language_code){
            //$iclTranslationManagement->make_duplicate($master_post_id, $lang);   // old one
			icl_makes_duplicates($master_post_id);  // sarah gave new one. 
        }
    }
     
    // re-hook this function
   add_action('save_post', 'wpml_duplicate_on_publish');
}

Is it what you mean?

but I got new error.

Fatal error: Call to undefined function icl_makes_duplicates() in /home/hosting_users/site/www/wp-content/themes/jobs/functions.php on line 1549

why it's not defined? i think it is. we put it on functions.php and wpml is installed.

Thanks,

January 29, 2014 at 11:51 am #329668

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

jin woo,

I think I did not explain myself very well and I managed to create a misunderstanding. Apologies for this.

You will not use the new function icl_makes_duplicates() inside the wpml_duplicate_on_publish() function. But rather the icl_makes_duplicates() should be used exclusively.

You do not need the wpml_duplicate_on_publish() function at all. You will rather call icl_makes_duplicates() in whatever function processes the post submission from your custom front-end form.

February 2, 2014 at 10:11 am #331757

jin woo

hello sarah,

thanks for your reply.. and I still have a problem.
I hope I understand what you told me. My understanding is that I don't need "wpml_duplicate_on_publish" and just add icl_makes_duplicates() on my front-end submission form.

This is what I have been doing with your tips.

1st of all I have added "icl_makes_duplicates()" on my front-end form-action file which insert posts.

like this..

icl_makes_duplicates($job_id);  //$job_id is that the post original id.

but it comes with an error saying.. "Fatal error: Call to undefined function icl_makes_duplicates() in .../functions.php on line 1517

so I added icl_makes_duplicates() function on functions.php


if(!function_exists('icl_makes_duplicates')){
	function icl_makes_duplicates( $master_post_id ) {
		global $sitepress, $iclTranslationManagement;
		if ( !isset( $iclTranslationManagement ) ) {
			$iclTranslationManagement = new TranslationManagement;  //Fatal error: Class 'TranslationManagement' not found in ..../functions.php on line 1529
		}
		$post_type = get_post_type( $master_post_id );
		if ( $sitepress->is_translated_post_type( $post_type ) ) {
			$iclTranslationManagement->make_duplicates_all( $master_post_id );    // error msg : Fatal error: Call to a member function make_duplicates_all() on a non-object in function.php
		}
	}
}

//icl_makes_duplicates('39217');  // this is for testing 39217 is a post id


I have got 2 errors.
1st one is on $iclTranslationManagement = new TranslationManagement; //Fatal error: Class 'TranslationManagement' not found in ..../functions.php

I just removed that part as a testing. (it's a if condition, so I just tried)

and next one has an error.

$iclTranslationManagement->make_duplicates_all( $master_post_id ); // error msg : Fatal error: Call to a member function make_duplicates_all() on a non-object in function.php

And somehow it duplicated as I wanted, but these get errors.

I believe I have missed 1 more function or class define.

Please let me know if you have any idea.

Thanks Dat and Sarah

February 3, 2014 at 9:40 am #332032

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

jin woo,

The function should not output any errors. This is worth looking into so could I possibly ask if it is possible to be given temporary access (wp-admin and FTP) to your site?

You will find the needed fields for this below the comment area when you log in to leave your next reply.
The information you will enter is private which means only you and I can see and have access to it.

February 3, 2014 at 10:00 am #332060

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

jin woo,

I just checked the WPML version you are using and it seems that the function is missing. I will email you the latest dev version to try.

No need for access details. Be sure to remove the function as you defined it in your theme's functions.php file.

Please stand by for my email and let me know if you have not received anything.

February 4, 2014 at 2:01 am #332650

jin woo

Thanks I got it, I will follow your step and get you back. you guys are great!!

February 4, 2014 at 6:38 am #332675

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Great! Let me know how you get on.

Thanks 🙂

February 5, 2014 at 7:58 am #333519

jin woo

hello Sarah,

I added only a function on submit page. (insert page)

icl_makes_duplicates($job_id);  //$job_id is the original post id

But I got this error.

Fatal error: Call to undefined method stdClass::make_duplicates_all() in /home/hosting_users/site/www/wp-content/plugins/sitepress-multilingual-cms/inc/functions.php on line 185

It doesn't even make any duplication.

I have been trying to find what the problem caused. the submit action page is with ajax. so I thought that It could a problem with getting wpml functions in ajax page.

But the page is basically working fine except this time to use (get, call) wpml function.

Because we included this one on the begining of codes to wp functions.

include $_SERVER["DOCUMENT_ROOT"]."/wp-load.php"; // it's on the top of ajax page for submit(insert) posts

As I mentioned it works fine but got an error with icl_makes_duplicates function.

so I tried another way. I put that function in functions.php directly for testing.

like this...

icl_makes_duplicates('29481'); // 29481 is a job-id which is post-id

It duplicated!! But it gives an error on front-end.

This is the error message
Fatal error: Class 'TranslationManagement' not found in /home/hosting_users/tutorinkorea/www/wp-content/plugins/sitepress-multilingual-cms/inc/functions.php on line 181

I thought the problem is that It's already duplicated and It keeps duplicating same post. because I put it on functions.php....

hmm.. this is what I have been doing. I will keep trying to find what is the problem.

Please also let me know if you have any idea.

Thanks all the time.

February 6, 2014 at 7:18 am #334354

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

jin woo,

I did not have the chance to work with the function myself. I still plan to do so and provide my feedback here.

In the meantime you may want to check this tutorial where the function is being used in a real world example: http://wpml.org/documentation/theme-compatibility/develop-multilingual-classifieds-sites/

Let me know if you find something there that helps you move forward. Thanks!

The topic ‘[Closed] auto duplicate on front-end form. and 3.02-a draft issue.’ is closed to new replies.