Chuyển đến nội dung Chuyển đến thanh bên

Open

Reported for: Advanced Custom Fields Multilingual 2.1.5

Topic Tags: Compatibility

Overview of the issue

When using Advanced Custom Fields (ACF) with WPML, you might experience issues with conditional logic not functioning correctly in secondary languages. This happens when the conditional logic depends on fields that use object-based or term-based references, such as taxonomy or post object fields.

Workaround

Please, make sure of having a full site backup of your site before proceeding.

  • Add the following code to your theme’s functions.php file.
    /**
     * WPML Workaround for compsupp-wpmltriage-3649 
     */
    add_action( 'init', function() {
    	if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { 
    		add_filter( 'acf/load_field', function ( $field ) {
    
    			if ( empty( $field['conditional_logic'] ) || ! is_array( $field['conditional_logic'] ) ) {
    				return $field;
    			}
    
    			foreach ( $field['conditional_logic'] as $group_index => $rules ) {
    				if ( ! is_array( $rules ) ) {
    					continue;
    				}
    
    				foreach ( $rules as $rule_index => $rule ) {
    					if ( isset( $rule['field'], $rule['value'] ) && is_numeric( $rule['value'] ) ) {
    						// Fetch the parent field
    						$parent_field = acf_get_field( $rule['field'] );
    						if ( ! isset( $parent_field['type'] ) ) {
    							continue;
    						}
    
    						if ( $parent_field['type'] === 'taxonomy' ) {
    							$rule['value'] = apply_filters( 'wpml_object_id', $rule['value'], $parent_field['taxonomy'], true );
    						} elseif ( $parent_field['type'] === 'post_object' ) {
    							$post_types = $parent_field['post_type'] ?? ''; // can be multiple 
    							if ( is_array( $post_types ) ) {
    								foreach ( $post_types as $post_type ) {
    									$translated_id = apply_filters( 'wpml_object_id', $rule['value'], $post_type, true );
    									if ( $translated_id && $translated_id !== $rule['value'] ) {
    										$rule['value'] = $translated_id;
    										break;								
    									}
    								}
    							} else {
    								$rule['value'] = apply_filters( 'wpml_object_id', $rule['value'], $post_types, true );
    							}
    						}
    						$field['conditional_logic'][ $group_index ][ $rule_index ] = $rule;
    					}
    				}
    			}
    
    			return $field;
    		}, 100 );
    	}
    } );
            

Để lại trả lời

Vui lòng giữ đúng chủ đề và tôn trọng người khác. Nếu bạn cần trợ giúp với các vấn đề không liên quan đến bài đăng này, hãy sử dụng Diễn đàn Hỗ trợ của chúng tôi để bắt đầu trò chuyện hoặc gửi vé hỗ trợ.

Bạn có thể sử dụng các thẻ này:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>