Categories
WordPress

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' );

58 replies on “Make Polylang WordPress plugin copy the content from the original post”

Hi,
This is exactly what I’m searching for but when I’ve added this to my WP, it broke down. It was just a white screen.

I’ve added your code to wp-includes/functions.php or should I add this to the function file from the theme?
I’m using Betheme on wp 4.1.7

Many thanks!

Hi Junaid, Thanks for this.. It worked nicely. However I have some fields that are not translating. Like name of the person posting, location etc.. Any ideas?

Hi Junaid,
Yes, you’re right. My fault. I’ve copied post without acf field ๐Ÿ˜‰
Thanks a lot once again. It’s saved me a lot of time ๐Ÿ™‚ Great piece of code ๐Ÿ™‚

Excellent code, but would it be better if it included a an “if” clause to check if Polylang plugin is active ? In case not, disregard rest of code. In this way, we load the code selectively

Hello,

I use a child theme and added the code you provided in the child theme’s functions.php, but I’m getting an internal server error.

If I add it to the parent theme functions.php, it would be removed if the theme is updated, wouldn’t it?

This is super usefull! thanks a lot!
I also miss this option in polylang…

Too bad we can’t copy content to existing translations…

Maybe one day ๐Ÿ™‚

Hello,

I added this filter but it messed my posts a bit. Is there a way to remove it? I tried remove_filter( ‘default_content’, ‘jb_editor_content’ ); but it keeps copying the content. Thank you.

Hi!

It works but not tottally. Im using WP JOB MANAGER and when im trying to duplicate all content, all fields from this plugin are not beging copyed. How to copy this content too?

Also, some one knows how to autoduplicate in all languages when a post is published?

Thanks a lot!

Hi, this tutorial is just to copy the content for the default WordPress editor. I’d suggest contacting the plugin developer for what you need.

Hello,

This is working for me but the galery of media is not going along with it unfortunately,
Any solution for it

Cheers

This tutorial is only for the content. The Pro version of Polylang allows you to copy all the content.

Boom. That worked great! Thanks. Yes this should be an option especially for those who use page builders.

It can be dangerous from an SEO point of view to possibly inherent text from the other language, but if you are careful this is a lot easier than recreating the page.

Thanks again.

Hello the code that copies the title works fin for me, but the one that should copy the content dosn’t. Does it support woocommerce products ?

Thanks in advance!

the wooCommerce short description is just a rebranding of the standard wordpress excerpt field so you can do it like this:
// Make sure Polylang copies the excerpt [woocommerce short description] when creating a translation
function hy_editor_excerpt( $excerpt ) {
// 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_excerpt;
}

return $content;
}
add_filter( ‘default_excerpt’, ‘hy_editor_excerpt’ );

thanks a lot
the code works great

is it possible that you publish a code for copying the excerpt.
the woo commerce code didn’t work for me

thanks
guy

First of all thank you so Much for your work.
I have one question though, could it be possible, when I add posts in one language that the extension copies the post in another language automatically?

Works great however I have multiple pages and when I change from English to French for example and then go to another page the new pages is in English and not translated.how can I fix this?

Hi, your code works like charm but on my site the property meta information is not being copied/imported over into the new language, any code suggestions how I can also get this to happen smoothly?
Thanks in advance

Hey Sabrina,
This is only to copy the content. I think the Pro version of Polylang should copy over the fields.

Amazing! Exactly what I have been looking for! It even copies content for other post types, for example Portfolio entry. Great work, mate.

Hello Junaid Bhura! Thank you so much for this! Works like a charm!
Just a notice, how can we do the same for all the woocomerce articles? This actually copies all the post info but not the prices and variations. Is there a way?

Thank you so much!

Still working great on WP 5.2 with custom Gutenberg blocks. You’ve saved me and my client a lot of time and trouble. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *