Skip to content

Instantly share code, notes, and snippets.

@tnottu
Created November 28, 2016 13:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tnottu/a28a5037487a7c7fec68c968e91f5568 to your computer and use it in GitHub Desktop.
Save tnottu/a28a5037487a7c7fec68c968e91f5568 to your computer and use it in GitHub Desktop.
WP Polylang Bulk Translate
<?php
/*
Plugin Name: Polylang Bulk Translate
Plugin URI:
Version: 0.1.0
Author: Tyomaa Oy
Author URI: https://github.com/tnottu
Description: Translate multiple posts with bulk actions
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: polylang-bulk-translate
*/
class PolylangBulkTranslate {
/**
* Constructor
*/
public function __construct() {
// Check that Polylang is active
global $polylang;
if (isset($polylang)) {
add_action( 'current_screen', array( $this, 'register_bulk_actions' ) );
}
}
/**
* Register "Translate to: $lang" -actions
*/
function register_bulk_actions() {
$is_any_language_active = !empty( pll_current_language() );
$currentScreen = get_current_screen();
$is_post_list = $currentScreen->base === 'edit';
$is_taxonomy_list = $currentScreen->base === 'edit-tags';
$post_type = $currentScreen->post_type;
$taxonomy = $currentScreen->taxonomy;
$type = $is_post_list ? 'post' : 'term';
// TODO: Add support for taxonomies
$is_translatable = $is_post_list && pll_is_translated_post_type( $post_type );
$should_init = $is_any_language_active && $is_translatable;
if (!$should_init) {
return;
}
$bulk_actions = new Seravo_Custom_Bulk_Action( array( 'post_type' => $post_type ) );
// Register action for each language, except current
foreach ( pll_languages_list() as $language ) {
if ($language === pll_current_language()) {
continue;
}
$bulk_actions->register_bulk_action( array(
'menu_text' => 'Translate to: ' . strtoupper( $language) ,
'admin_notice' => ( $type === 'post' ) ? '%s posts translated.' : '%s terms translated.',
'callback' => function( $ids ) use ( $language, $type ) {
foreach ( $ids as $id ) {
if ( $type === 'post' ) {
$this->translate_post( $id, $language );
} else {
// TODO: Add support for taxonomies
}
}
return true;
}
) );
}
$bulk_actions->init();
}
/**
* Translate a post
*
* Creates a copy of the post, with all possible content from the original and sets
* the new copy as a translation.
*
* @param int $post_id ID of the original post
* @param string $new_lang New language slug
*
*/
function translate_post( $post_id, $new_lang ) {
$from_post = get_post( $post_id );
$has_translation = pll_get_post( $post_id, $new_lang );
if ($has_translation) return;
$new_post = clone $from_post; // Copy the post
/*
* Prepare post
*/
$new_post->ID = null;
$new_post->post_status = 'draft';
$new_post_id = wp_insert_post( $new_post ); // Creates a new post thanks to ID being null
/*
* Set languate & translation relation
*/
pll_set_post_language( $new_post_id, $new_lang );
pll_save_post_translations( array(
pll_get_post_language( $from_post->ID ) => $from_post->ID,
$new_lang => $new_post_id
) );
/*
* Copy relevant extra data
*/
PLL()->sync->copy_taxonomies( $from_post->ID, $new_post_id, $new_lang );
PLL()->sync->copy_post_metas( $from_post->ID, $new_post_id, $new_lang );
wp_update_post( $new_post_id );
}
}
add_action('plugins_loaded', create_function('', 'global $polylang_bulk_translate; $polylang_bulk_translate = new PolylangBulkTranslate();'));
@cedriccharles4
Copy link

Hello @tnottu :) !

Thank you for that :) !

After installing and activate it, I have en error 500 when I go to a custom post type listing page in the admin saying :
PHP Fatal error: Uncaught Error: Class 'Seravo_Custom_Bulk_Action' not found in /Users/cedricnoomia/Sites/mistergenius/wp-content/plugins/a28a5037487a7c7fec68c968e91f5568-520ff2bbef6910c43043f34bd2f9b3c97dcbe45f/polylang-bulk-translate.php:52 Stack trace: #0 /Users/cedricnoomia/Sites/mistergenius/wp-includes/class-wp-hook.php(298): PolylangBulkTranslate->register_bulk_actions(Object(WP_Screen)) #1 /Users/cedricnoomia/Sites/mistergenius/wp-includes/class-wp-hook.php(323): WP_Hook->apply_filters(NULL, Array) #2 /Users/cedricnoomia/Sites/mistergenius/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #3 /Users/cedricnoomia/Sites/mistergenius/wp-admin/includes/class-wp-screen.php(393): do_action('current_screen', Object(WP_Screen)) #4 /Users/cedricnoomia/Sites/mistergenius/wp-admin/includes/screen.php(232): WP_Screen->set_current_screen() #5 /Users/cedricnoomia/Sites/mistergenius/wp-admin/admin.php(187): set_current_screen() #6 /Users/cedricnoomia/Sites/mistergenius/wp-admin/edit.php(10): require_once('/Users/cedricno...') #7 in /Users/cedricnoomia/Sites/mistergenius/wp-content/plugins/a28a5037487a7c7fec68c968e91f5568-520ff2bbef6910c43043f34bd2f9b3c97dcbe45f/polylang-bulk-translate.php on line 52

@masato-igarashi
Copy link

@cedriccharles4
this plugin require Custom Bulk Actions plugin
https://github.com/Seravo/wp-custom-bulk-actions

@goutsdeweb
Copy link

goutsdeweb commented Mar 30, 2018

Hello,
I need to translate around 300 woocommerce products but don't understand how to use it. Do I need to add code in functions.php ?

Thanks

Finally found the option, don't understand why it doesn't appear the first time.

@amityweb
Copy link

Dos this copy or sync? Any way this can be advanced to include Sync to Languages too, not just copy?

@amityweb
Copy link

Found a serious issue with this.... Using this plugin worked for the new language, but it lost all connections between all the other languages we have already translated.

Background: we used WPML previously, then used a WPML to Polylang migration plugin which worked fine. All pages were there in Polylang, all the languages connected (so you see the Edit icon). When we then created a new language (and still the connections existed) then went and did a Bulk Translate to the new language, it worked for that language, but then all other languages lost the connections. The pages exist in all languages, but there is no link between them (so it shows the + symbol now, not the edit symbol).

So this is quite a serious issue for us. Dont know if its do with the above script, or the way the languages are migrated from WPML to Polylang. But it all worked OK before using this script. I did it twice in my dev site, happened both times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment