Support

Account

Home Forums General Issues How to display default language Options field, when no WPML translation exists

Solving

How to display default language Options field, when no WPML translation exists

  • So we have an ACF Options page with a lot of site options (text for buttons, contact details, phone numbers etc).

    We have installed WPML as we will have many country websites.

    Our process will be to duplicate all content on the English site to create a new language site, so all content initially in say German will show English. The customer will go through and translate it over time.

    BUT, the Options values are not translated, they all are blank in the new language site. This means when we visit the new language site, all the buttons are blank, no contact info, no phone numbers etc. It looks like we have to fill in all these fields (there are many of them) to get a new language site to work.

    Is there a simple way to duplicate all options field values into other languages?

    If not, does anyone know how to retrieve the default language option values if there are no values for the language the user is on? So that way it will always display English until the customer fills in the fields. We would need it for the_field and the_sub_field within the has_rows(); the_row() loop.

    Thanks

  • Hi @infoamitywebsolutions-co-uk

    The options page has different values between the master and translated version to make sure that you can set different values for them. The option values are saved in the wp_options table for both site. For the master site, it’s saved with this option name:

    options_field_name

    Where “field_name” is your field name. For the translated version, it uses the country iso code like this:

    options_de_field_name

    To get the option values from the master version, you need to set the current language with the master site iso code. You can also automatically do it by using the acf/load_value. It should be something like this:

    function my_acf_load_value( $value, $post_id, $field )
    {
        if( $post_id == 'options_de' && !$value ){
            
            //set the current language to EN temporarily
            add_filter('acf/settings/current_language', function(){return 'en';});
    
            $value = get_field($field['name'], 'options', false);
    
            //set it back to DE
            add_filter('acf/settings/current_language', function(){return 'de';});
            
        }
        
        return $value;
    }
    
    // acf/load_value - filter for every value load
    add_filter('acf/load_value', 'my_acf_load_value', 20, 3);

    I hope this helps 🙂

  • This is great, thanks a lot, will give it a go.

    But are we able to tell if a field is a sub-field? Some of my values use get_sub_field() so not sure how to tell if the field needs get_field or get_sub_field. The above code is not working on all my values just because some use get_sub_field but the code uses only get_field

    Saying that, it doesn’t work when changing it to get_sub_field anyway. Is it because its in a loop? Here is one such code for example, that I need to display the English content if the translation does not exist.

    <?php
    if( have_rows('enquire_link', 'option') ):
    	while( have_rows('enquire_link', 'option') ): the_row();
    ?>
    		<a href="<?php the_sub_field('link'); ?>" class="enquire-button">
    			<?php the_sub_field('text'); ?>
    		</a>
    <?php
    	endwhile;
    endif;
    ?>

    So in the above, the have_rows() check is false, nothing in the loop is being executed so probably does not even get to your acf/load_value hook.
    Thanks

  • Hi @infoamitywebsolutions-co-uk

    In this kind of situation, you can change the current language before the repeater code instead. Should be something like this:

    <?
    
    <?php
    add_filter('acf/settings/current_language', function(){return 'en';});
    
    if( have_rows('enquire_link', 'option') ):
        while( have_rows('enquire_link', 'option') ): the_row();
    ?>
            <a href="<?php the_sub_field('link'); ?>" class="enquire-button">
                <?php the_sub_field('text'); ?>
            </a>
    <?php
        endwhile;
    endif;
    
    add_filter('acf/settings/current_language', function(){return 'de';});
    ?>

    But I think the best way would be copying the saved data from options_field_name to options_de_field_name.

    I hope this makes sense 🙂

  • Thanks but I dont think this works… we want to run have_rows() in the current language the user is on, because there could be options added in that language, in which case we want to show them. Only when there are no values in that language do we want to show the english. This is because WPML is not copying the site options so they are all blank.

    So putting the language change before have_rows() wont work, because it will always return the english wont it?

    I was hoping to find some conditional that says if the value does not exist to get the value from the english.

    Thanks

  • I am trying to create my own function as follows, to replace the_sub_field() in the template, but it returns the value twice.

    function the_sub_field_wpml($field)
    {
    	$the_value = the_sub_field($field);
    	
    	if($the_value == '' || !$the_value)
    	{
    	
            //set the current language to EN temporarily
            add_filter('acf/settings/current_language', function(){return 'en';});
    
            $the_value = the_sub_field($field);
    
            //set it back to DE
            add_filter('acf/settings/current_language', function(){return 'de';});	
    	
    	}
    	
    	
    	return $the_value;
    }
  • Your original code above seems to be working OK if I change:

    get_sub_field('text');

    to

    echo the_sub_field('text');

    I guess the_sub_field() uses get_field() but get_sub_field() doesn’t?

    So working Ok now!

  • Hi @infoamitywebsolutions-co-uk

    Please keep in mind that the_sub_field() will echo the value automatically, so you don’t have to use echo anymore.

    Also, the best thing to do in this kind of situation would be creating a script that will copy the values from the master site to the translated page. You can list the fields name you use for the options page, loop through it and update the translated site like this:

    $options_field_names = array('field_name_1','field_name_2', 'field_name_3');
    
    foreach( $options_field_names as $options_field_name ){
        $master_value = get_field($options_field_name, 'options', false);
        update_field($options_field_name, $master_value, 'options_de_');
    }

    I hope this helps 🙂

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘How to display default language Options field, when no WPML translation exists’ is closed to new replies.