Skip to content

Instantly share code, notes, and snippets.

@willybahuaud
Last active January 6, 2022 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willybahuaud/c48b2499dffd9c235c20176c7eda9542 to your computer and use it in GitHub Desktop.
Save willybahuaud/c48b2499dffd9c235c20176c7eda9542 to your computer and use it in GitHub Desktop.
Automatic posts duplication across languages (polylang pro)
<?php
/**
* Cette action permet lors d'une sauvegarde d'article, de le dupliquer automatiquement dans toutes les langues
*/
add_action( 'pll_save_post', 'w_force_duplicate_post', 10, 3 );
function w_force_duplicate_post( $post_id, $post, $translations ) {
remove_filter( 'pll_save_post', 'w_force_duplicate_post' );
if ( 'event' != get_post_type( $post ) ) {
return false;
}
$langs = pll_languages_list();
$polylang = PLL();
$sync_model = new \PLL_Sync_Post_Model( $polylang );
foreach ( $langs as $l ) {
if ( empty( $translations[ $l ] ) ) {
$sync_model->copy_post( $post_id, $l, true );
}
}
add_filter( 'pll_save_post', 'w_force_duplicate_post', 10, 3 );
}
/**
* Et ça c'est juste un crochet pour appliquer la duplication à tous les contenus qui existaient déjà
*/
add_action( 'admin_post_force_duplicate_events', 'w_go_duplicate' );
function w_go_duplicate() {
$events = get_posts( array(
'post_type' => 'event',
'posts_per_page' => -1,
'suppress_filters' => true,
) );
foreach ( $events as $ev ) {
do_action( 'save_post', $ev->ID, $ev, true );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment