• Resolved Ulrich

    (@grapplerulrich)


    Hi! ??

    You have added WPML support. If you wanted to add Polylang support, you could use the following code:

    function duplicate_post_polylang_init() {
    	if ( function_exists( 'pll_get_post_translations' ) ) {
    		add_action( 'dp_duplicate_page', 'duplicate_post_polylang_copy_translations', 10, 3 );
    		add_action( 'dp_duplicate_post', 'duplicate_post_polylang_copy_translations', 10, 3 );
    	}
    }
    add_action( 'admin_init', 'duplicate_post_polylang_init' );
    
    /**
     * Add support for duplicate posts for Polylang. Duplicate all languages together.
     *
     * @param int     $new_post_id   Post ID of new cloned post.
     * @param WP_Post $original_post WP_Post object of the original post.
     * @param string  $status        Post status of new cloned post.
     */
    function duplicate_post_polylang_copy_translations( $new_post_id, $original_post, $status = '' ) {
    	remove_action( 'dp_duplicate_page', 'duplicate_post_polylang_copy_translations', 10 );
    	remove_action( 'dp_duplicate_post', 'duplicate_post_polylang_copy_translations', 10 );
    
    	$languages = pll_get_post_translations( $original_post->ID );
    	$post_ids  = [];
    	foreach ( $languages as $slug => $post_id ) {
    		// Do not duplicate post again.
    		if ( $original_post->ID === $post_id ) {
    			$post_ids[ $slug ] = $new_post_id;
    			continue;
    		}
    		$post_ids[ $slug ] = duplicate_post_create_duplicate( get_post( $post_id ), $status );
    	}
    	pll_save_post_translations( $post_ids );
    }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Polylang Support’ is closed to new replies.