Skip to content

Instantly share code, notes, and snippets.

@webdados
Last active January 11, 2017 09:41
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 webdados/abea6f2550f90ca5b49f463cb932453b to your computer and use it in GitHub Desktop.
Save webdados/abea6f2550f90ca5b49f463cb932453b to your computer and use it in GitHub Desktop.
When using CMB2 and WPML it may be useful to style "copy" fields differently, on translated posts edit screen, so the user knows it makes no sense to edit them.
<?php
add_action( 'admin_head', 'style_cmb2_copy_fields_as_readonly', 9999 );
function style_cmb2_copy_fields_as_readonly() {
$screen = get_current_screen();
//var_dump($screen);
if ( $screen->base == 'post' ) {
global $post, $sitepress;
if ( isset($post) && isset($sitepress) ) {
//Original post or translation?
$original_post = false;
if ( $trid = $sitepress->get_element_trid( $post->ID, 'post_'.$post->post_type ) ) {
if ( $translations = $sitepress->get_element_translations( $trid, 'post_'.$post->post_type ) ) {
foreach( $translations as $translation ) {
if ( $translation->element_id == $post->ID && is_null($translation->source_language_code) ) {
//Original post
$original_post = true;
break;
}
}
//Not the original post? Go ahead
if ( !$original_post ) {
$readonly_fields = array();
$translation_management = $sitepress->get_setting( 'translation-management', array() );
foreach( $translation_management['custom_fields_translation'] as $key => $value ) {
if ( intval($value) == 1 && substr($key,0,1) != '_' ) {
$readonly_fields[] = $key;
}
}
if ( count($readonly_fields)>0 ) {
?>
<style type="text/css">
<?php
foreach($readonly_fields as $field) {
?>
.cmb2-id-<?php echo str_replace( '_', '-', sanitize_html_class( $field ) ); ?> {
opacity: 0.25;
}
<?php
}
?>
</style>
<?php
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment