Due to the large volume of support requests, you may experience longer wait until a supporter handles your request. We apologize for the inconvenience this causes. We do not have any recurring unknown issues so we encourage you to please search the forum for other support topics that might be helpful and to review our documentation and look at our erratas page for known issues. A supporter will respond to you as soon as possible.

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.

You are not logged in. You can view support threads, but not post.
If you are already logged in, please refresh your browser.

This topic contains 14 replies, has 7 voices, and was last updated by  jin woo 3 years, 10 months ago. The support staff assigned to this topic is sarah.n.

Author Posts
Author Posts
August 7, 2013 at 12:53 pm #152735

Tim

Hi there,
I know this has been asked before, but none of the existing threads gave me a definitive answer (although most of them are marked as resolved).
<a href="http://wpml.org/forums/topic/automatic-duplication-of-a-post/">http://wpml.org/forums/topic/automatic-duplication-of-a-post/</a&gt;
< href="http://wpml.org/forums/topic/remove-redirection-on-single-language-custom-post-type/page/2/">http://wpml.org/forums/topic/remove-redirection-on-single-language-custom-post-type/page/2/</a&gt;

How can I implement the functionality of an automatic duplication upon publishing/creating of a post/page in the main language? I know it is possible to manually duplicate posts/pages through the translation management plugin, but that is not what I need.

Here is what I tried so far (with the help of the second thread that I posted above):

In my functions.php I create a new function called "duplicate_on_publish" where I include the "translation-management.class.php" file. After that I try to call the "make_duplicates_all();" function with the current post-ID. This "duplicate_on_publish" function is then added on the pending_to_publish, draft_to_publish, new_to_publish and save_post hooks.

This is how it looks like:

function duplicate_on_publish ( $post_id ) {
include(ABSPATH.'wp-content/plugins/sitepress-multilingual-cms/inc/translation-management/translation-management.class.php');
$class = new TranslationManagement();
$class->make_duplicates_all($post_id);
}

add_action('pending_to_publish', 'duplicate_on_publish');
add_action('draft_to_publish', 'duplicate_on_publish');
add_action('new_to_publish', 'duplicate_on_publish');
add_action('save_post', 'duplicate_on_publish');

This gives me an error about not being able to redefine the variables 'ICL_TM_NOT_TRANSLATED', 'ICL_TM_WAITING_FOR_TRANSLATOR' etc. And also about not being able to redeclare the TranslationManagement(); class. (That's also what Tomas mentioned in the "resolved thread" above).

So please, could you give me a hint on where to go with this? Just saying "hook the make_duplicate(); function to the save_post hook" doesn't help me that much.

I know this has been asked and done before, so I (and I guess several other people) would gladly have a definitive answer on how to do this whithin our functions.php.

Thanks for any help.

August 7, 2013 at 3:30 pm #152811

Bruno
Forum moderator

Supporter languages: English (English ) Portuguese (Brazil) (Português )

Supporter Timezone: America/Sao_Paulo (GMT-02:00)

Dear Tim,

Please try this:

function duplicate_on_publish ( $post_id ) {
     //checks if the class does not exist.
     if(!class_exists('TranslationManagement')){
          include(ABSPATH.'wp-content/plugins/sitepress-multilingual-cms/inc/translation-management/translation-management.class.php');
     }

     $class = new TranslationManagement();
     $class->make_duplicates_all($post_id);
}

add_action('pending_to_publish', 'duplicate_on_publish');
add_action('draft_to_publish', 'duplicate_on_publish');
add_action('new_to_publish', 'duplicate_on_publish');
add_action('save_post', 'duplicate_on_publish');

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Thank you.

August 7, 2013 at 3:40 pm #152818

Tim

That didn't work at all. The whole wordpress site crashed when I inserted the code. Additionally I don't think my code is working at all, since I just copy pasted the stuff from the other thread.

Could you tell me, how I can call the functions from the translation-management.class.php? Do I really have to set up a new TranslationManagement(); class to use the functions?

I also read somewhere that the save_post hook is not optimal for this since it is actually called on creation before the post is saved for the first time (auto-save). Do you have any insight on that? That's why I went for the pending_to_publish etc. hooks.

Thanks in advance.

August 7, 2013 at 8:49 pm #152904

Bruno
Forum moderator

Supporter languages: English (English ) Portuguese (Brazil) (Português )

Supporter Timezone: America/Sao_Paulo (GMT-02:00)

Dear Tim,

Please, try this one:

function duplicate_on_publish( $post_id ) {

     remove_action('save_post', 'duplicate_on_publish');

     //checks if the class does not exist.
     if(!class_exists('TranslationManagement')){
          include(ABSPATH.'wp-content/plugins/sitepress-multilingual-cms/inc/translation-management/translation-management.class.php');
     }
 
     $class = new TranslationManagement();
     $class->make_duplicates_all($post_id);

     add_action('save_post', 'duplicate_on_publish');
}
 
add_action('pending_to_publish', 'duplicate_on_publish');
add_action('draft_to_publish', 'duplicate_on_publish');
add_action('new_to_publish', 'duplicate_on_publish');
add_action('save_post', 'duplicate_on_publish');

Please let me know if this works.

August 8, 2013 at 8:05 am #152970

Tim

Well thanks for the effort Bruno, but that didn't work either. Also it doesn't really make any sense... you call the function on the save_post hook and then remove and add it within the function again. It's pretty much the same as before.

Seriously, I know this has been asked several times before:
How can one auto-duplicate posts on creation without having to do a batch duplication manually. I know the code I posted is flawed and it isn't just the wrong hook that is missing.

Could you kindly forward this to your developers? I'm betting they had this question asked multiple times and have to have a solutions for this.

Best Regards

August 8, 2013 at 4:38 pm #153160

Bruno
Forum moderator

Supporter languages: English (English ) Portuguese (Brazil) (Português )

Supporter Timezone: America/Sao_Paulo (GMT-02:00)

Dear Tim,

I will forward your ticket for Sarah.

Just to inform the remove_action and add_action, it is useful for not create a loop within the save_post action:
http://codex.wordpress.org/Plugin_API/Action_Reference/save_post#Avoiding_infinite_loops

I hope that makes more sense now.

Thank you.

August 9, 2013 at 6:59 am #153236

Tim

Thanks Bruno for your support. Still I don't think my code is anywhere close to being useful, since I pretty much just copy pasted it from a thread that is close to a year old.

Best regards

August 9, 2013 at 10:32 am #153327

sarah.n
Forum moderator

Supporter languages: English (English )

Hello Tim,

This is Sarah. Bruno forwarded you forum thread to me so I will be assisting you from here. Hope this is OK with you.

You asked: "How can I implement the functionality of an automatic duplication upon publishing/creating of a post/page in the main language?"

Are creating a post/page from within the admin or programmatically?

August 9, 2013 at 10:53 am #153339

Tim

Hello Sarah,
thanks for assisting me with my problem.

The original question was more in terms of how to auto-duplicate posts/pages that are created within the admin area. But since you asked, some of the posts are additionally imported/created/deleted through a CSV Import Plugin (I opened another thread on this issue). So I actually need both (although I thought there might be some hook where one could call the wpml functions, like "on post publish" or something).

Thanks in advance

August 9, 2013 at 11:16 am #153344

sarah.n
Forum moderator

Supporter languages: English (English )

OK let me look into this and will get back to you here as soon as I have something for you.

August 9, 2013 at 2:58 pm #153430

sarah.n
Forum moderator

Supporter languages: English (English )

Tim,

Could you try this in your theme's functions.php file and let me know how it works for you?

/**
 * 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');
}

This will handle auto-duplication when you create a post/page/cpt from within your admin.

A little glitch I noticed while testing it is that the moment you click on "Add new": /wp-admin/post-new.php you will see that WPML displays a "This document is a duplicate of and it is maintained by WPML." message in the top right "Language" panel. Ignore it,create your post and publish. After this while still on the original edit post screen click on "Translate independently" to see the duplicate translations.

Let me know how you get on with this.

August 14, 2013 at 2:03 am #154570

Jason

Hi Sarah, I found this post and it is pretty much exactly what I was looking for as I am also using a plugin that imports and xml file and creates post. The above code worked for me but is it possible to only have it auto-duplicate post and not pages or Custom Post Types?

August 26, 2013 at 3:56 pm #157740

Keijo

Jason, for posts only, I think, you should add e.g. if($post->post_type ! = 'post') after revision check with if-statement & return.
That's what I also needed.

Sarah, I was looking for the exactly same functionality for being able to auto-duplicate when creating/editing posts via xmlrpc. My test site (localhost) is using two languages. WPML version is quite new 2.9.1, downloaded about 3 weeks ago. WordPress version is 3.5.2.

Your code works without problems when creating posts manually from UI.

However, when creating posts via xmlrpc, it seems category will be set to "uncategorized" for original and duplicated posts. I've tried single category and multiple categories set to post without success. Category has a translation available. The same post created via xmlrpc is shown correctly with correct category when your duplicate code is not enabled (commented out).

Because of this issue, I believe the code you provided is not complete.

Do you have any idea what's still missing?

November 27, 2013 at 8:44 am #298589

Yvo

Had any one tried to update and save the menu after inserting this code??? it gets duplicated in main language as many time as you have available languages.

January 27, 2014 at 2:43 am #327939

jin woo

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

I have this error from my front end - posting form.

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

Any solution? I will open 1 ticket for it.

The topic ‘[Closed] Auto duplicate posts/pages on publish’ is closed to new replies.