Skip to content

Instantly share code, notes, and snippets.

@mattkeys
Last active March 1, 2022 18:24
Show Gist options
  • Save mattkeys/01ef26935c8a0527cf9dcee71880e54d to your computer and use it in GitHub Desktop.
Save mattkeys/01ef26935c8a0527cf9dcee71880e54d to your computer and use it in GitHub Desktop.
WPML doesn't properly handle field groups that are assigned to page parents. This filter fixes that.
// Make WPML compatible with ACF field groups with location set to 'page parent'
function filter_acf_location_rule_match_param( $result, $rule, $screen, $field_group ) {
$default_lang = apply_filters( 'wpml_default_language', NULL );
if ( ! isset( $screen['lang'] ) || $default_lang == $screen['lang'] ) {
return $result;
}
if ( ! isset( $screen['post_id'] ) || empty( $screen['post_id'] ) ) {
return $result;
}
$default_lang_post_id = apply_filters( 'wpml_object_id', $screen['post_id'], $screen['post_type'], false, $default_lang );
if ( ! $default_lang_post_id ) {
return $result;
}
$default_lang_post_parent_id = wp_get_post_parent_id( $default_lang_post_id );
if ( ! $default_lang_post_parent_id ) {
return $result;
}
switch ( $rule['operator'] ) {
case '==':
if ( $default_lang_post_parent_id == $rule['value'] ) {
$result = true;
}
break;
case '!=':
if ( $default_lang_post_parent_id != $rule['value'] ) {
$result = true;
}
break;
}
return $result;
};
add_filter( 'acf/location/match_rule/type=page_parent', 'filter_acf_location_rule_match_param', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment