Skip Navigation

[Resolved] Automatically Copy Images in Different Languages Types + WPML no longer working

This support ticket is created 6 years, 8 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Christian Cox 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#908367

th

I went to the archive of old support ticket, and I found this link (https://wpml.org/forums/topic/how-to-ensure-new-posts-have-duplicates-created-automatically/).

Basically, I added this code to the functions.php in addition to the other previous code mentioned above, and it seems to be working fine now.

I just wanted to double check with you to see that all these three codes used in combination, won't cause any further problem. I'd appreciate if you could look over!

add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
function my_duplicate_on_publish( $post_id ) {
    global $post;
    // don't save for autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    // dont save for revisions
    if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
        return $post_id;
    }
   
    if ($post->post_type=='your-post-type') {
          
        //we need this to avoid recursion see add_action at the end
        remove_action( 'wp_insert_post', 'my_duplicate_on_publish' );
   
        // make duplicates if the post being saved
        // #1. itself is not a duplicate of another or
        // #2. does not already have translations
   
        $is_translated = apply_filters( 'wpml_element_has_translations', '', $post_id, $post->post_type );
   
        if ( !$is_translated ) {
            do_action( 'wpml_admin_make_post_duplicates', $post_id );
        }
   
       //must hook again - see remove_action further up
       add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
  
    }
  
}
#908369

Hi, I would like to get an updated clone of your site if possible so I can run some additional tests and determine why the previous code stopped working. Please provide login credentials in the private reply fields here, or include a download link where I can download a Duplicator package.

#908963

Okay thanks, I don't see any obvious errors or cyclical function calls. I see that the new code actually does the post duplication part, and the code I originally provided was not set up to handle that. Looks okay to me, if it's working for you I think stick with it.