WordPress

Make Polylang WordPress plugin copy the content from the original post

By April 23, 2015 49 Comments
Make Polylang WordPress plugin copy the content from the original post

If you’re like me, you love Polylang!

I’ve used WPML in the past, and Polylang makes it look like an oversized overpowering slowpoke. What I hate the most about WPML is the fact that it is not “fully compatible” with Advanced Custom Fields, which is stupid to put it politely.

There is one problem with Polylang, however. Which is the fact that it does not copy the title and content when you create a new translation. I don’t fully agree with the reasoning behind this. There should at least be an option to copy the content from the original post to the translation.

But don’t worry, the fix is easy! Just add this in your theme’s functions.php file and the problem is fixed:

// Make sure Polylang copies the content when creating a translation
function jb_editor_content( $content ) {
    // Polylang sets the 'from_post' parameter
    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', 'jb_editor_content' );

Similarly, if you want to copy the title as well, use the following code:

// Make sure Polylang copies the title when creating a translation
function jb_editor_title( $title ) {
    // Polylang sets the 'from_post' parameter
    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', 'jb_editor_title' );

49 Comments

Leave a Reply