Skip to content

Instantly share code, notes, and snippets.

@tnottu
Created April 12, 2017 13:47
Show Gist options
  • Save tnottu/b5d84f5bbfc7fd79c3e3b9db575a241f to your computer and use it in GitHub Desktop.
Save tnottu/b5d84f5bbfc7fd79c3e3b9db575a241f to your computer and use it in GitHub Desktop.
Polylang: Copy ACF fields from default language
/*
* Copy meta fields across languages
*/
$acf_keys_to_copy = [
'acf_singular_field_name',
'acf_repeater_field_name' => ['acf_repeater_subfield_name']
];
add_filter( 'pll_copy_post_metas', function( $keys, $sync, $from, $to, $lang ) use ( $acf_keys_to_copy ) {
if ( pll_get_post_language( $from ) === pll_default_language() ) {
$keys = array_merge( $keys, get_copyable_meta_keys( $from, $to, $acf_keys_to_copy ) );
}
return $keys;
}, 10, 5 );
function get_copyable_meta_keys( $from, $to, $acf_keys_to_copy ) {
$keys_to_copy = [];
// Get custom meta keys from source post. Also get keys from destination post,
// allowing to synchronize deleted fields.
$all_post_meta_keys = array_unique( array_merge(
array_keys( get_post_custom( $from ) ),
array_keys( get_post_custom( $to ) )
) );
// Divide singular/repeater fields into separate arrays
foreach ( $acf_keys_to_copy as $key => $value ) {
if ( is_array( $value ) ) {
$acf_keys_to_copy_repeater[$key] = $value;
$acf_keys_to_copy_singular[] = $key;
} else {
$acf_keys_to_copy_singular[] = $value;
}
}
// Loop through all post meta keys to determine which whould be copied
foreach ( $all_post_meta_keys as $post_meta_key ) {
if ( in_array( $post_meta_key, $acf_keys_to_copy_singular ) ) {
// Normal, singular keys
$keys_to_copy[] = $post_meta_key;
} else {
// Repeater fields.
// Check if the post meta key matches the key structure of a syncable ACF repeater -field.
foreach ( $acf_keys_to_copy_repeater as $acf_field_key => $acf_subfield_keys ) {
$post_meta_key_matches_acf_repeater = false;
foreach ( $acf_subfield_keys as $acf_subfield_key ) {
if ( preg_match( "#{$acf_field_key}_[0-9+]_{$acf_subfield_key}$#", $post_meta_key ) ) {
$post_meta_key_matches_acf_repeater = true;
break;
}
}
if ( $post_meta_key_matches_acf_repeater ) {
$keys_to_copy[] = $post_meta_key;
break;
}
}
}
}
return $keys_to_copy;
}
// Make copyable fields read-only for translations
add_action('admin_head', function() use ( $acf_keys_to_copy ) {
global $post;
if ( get_current_screen()->id === 'my_custom_post_type' && pll_get_post_language( $post->ID ) !== pll_default_language() ) {
$field_keys = [];
// Get field keys
foreach ( $acf_keys_to_copy as $key => $value ) {
if ( is_array( $value ) ) {
$field_keys[] = $key;
} else {
$field_keys[] = $value;
}
}
$css = '<style>';
foreach ( $field_keys as $i => $key ) {
$css .= '.acf-field[data-name="' . $key . '"]' . ( next( $field_keys ) ? ',' : '' );
}
$css .= ' { pointer-events: none; opacity: 0.3; -webkit-user-select: none; user-select: none; cursor: not-allowed; }';
$css .= '</style>';
echo $css;
}
} );
@webboxnew
Copy link

Thank you so much for writing this. This is exactly what I needed + it prevent editing the acf in the secondary language. This is really really nice. You really made my day !

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