Article du fil d'actualités de CreaWeb2B

N'hésitez pas à commenter et partager

Polylang – Duplicating content from original post across translations

by | 5 Mar,2018 | Plugins, WORDPRESS

This post can be helpful for anybody using Polyang plugin to handle translations.

Working on original content (text, medias) is a time saver for translation work; but in the free version of Polylang, you can’t duplicate content or title from original post across translation. This lack always pop up when comparing polylang with WPML.

Trying to solve this problem, i came across a post wrote in english by Junaid Bhura who wrote a code snippet allowing to duplicate content and title from posts you want to translate with Polylang.

To add this additionnal code, you’d better use a child theme (if you don’t know how to create one, a very simple tutorial is available by following this link.)

To duplicate original post content, add the following code snippet to functions.php file of your child theme :

// Duplicate post content from original across translation
function cw2b_content_copy( $content ) {    
    if ( isset( $_GET['from_post'] ) ) {
        $my_post = get_post( $_GET['from_post'] );
        if ( $my_post )
            return $my_post->post_content;
    }
    return $content;
}
add_filter( 'default_content', 'cw2b_content_copy' );

if you also want to duplicate post title, add the following code snippet to functions.php file of your child theme :

// Duplicate post title from original across translation
function cw2b_editor_title( $title ) {    
    if ( isset( $_GET['from_post'] ) ) {
        $my_post = get_post( $_GET['from_post'] );
        if ( $my_post )
            return $my_post->post_title;
    }
    return $title;
}
add_filter( 'default_title', 'cw2b_editor_title' );

For Divi theme users, when you’ll add a translation, you will see a lot of shortcodes inside the wordpress editor.

You just have to click on “Use The Divi Builder” to activate it and see your content as sections, rows, columns and modules”

Feel free to comment and ask for help if needed.