Support

Account

Home Forums Add-ons Options Page WPML and ACF Options Reply To: WPML and ACF Options

  • For this issue, I wrote a little solution to use ACF options to work more easily with WPML

    The fields name

    First you have to set yours fields name for the option page in default language (or global) like

    • my_field

    For other language options pages (see ICL_LANGUAGE_CODE code return), set the field like this :

    • my_field_fr
    • my_field_en

    The functions

    
    function get_field_wpml( $field_key, $post_id = false, $format_value = true ) {
    
        global $sitepress;
    
        $is_cascade   = $post_id == 'option' && $format_value == true ? true : false;
        $format_value = $post_id == 'option' ? true : $format_value; // force $format_value = true for option
    
        // get field for default language
        elseif ( ( $sitepress->get_default_language() == ICL_LANGUAGE_CODE ) && ( $ret = get_field( $field_key, $post_id, $format_value ) ) ) {
           return $ret;
        }
    
        // get field for current language
        if ( $ret = get_field( $field_key . '_' . ICL_LANGUAGE_CODE, $post_id, $format_value ) ) {
            return $ret;
        }
    
        // get field when if not exists for locale by cascade
        elseif ( $is_cascade ) {
            return get_field( $field_key, $post_id, $format_value );
        }
    
        return false;
    }
    
    
    function have_rows_wpml( $field_key, $post_id = false ) {
    
        global $sitepress;
    
        if ( $sitepress->get_default_language() == ICL_LANGUAGE_CODE ) {
           return have_rows( $field_key, $post_id );
        }
    
        return have_rows( $field_key . '_' . ICL_LANGUAGE_CODE, $post_id );
    }
    

    The second function need to be enhanced to support cascade for quering

    Examples of use

    1/ To get an option value for the current language, and if not, for the default language (cascade)

    
    $my_field = get_field_wpml( 'my_field', 'option' ); 
    

    2/ To get an option value ONLY for the current language (no cascade) – Set the third parameter to false

    
    $my_field = get_field_wpml( 'my_field', 'option', false ); 
    

    3/ To iterate on repeater or flexible content, use like this :

    
    while ( have_rows_wpml( 'my_repeater_field', 'option' ) ):
        the_row();
        $my_sub_field = get_sub_field('my_sub_field', 'option');
    endwhile;