Support

Account

Home Forums ACF PRO acf_form field instructions translations

Solved

acf_form field instructions translations

  • Hi,
    I’m working on a frontend form using qtranslate-x plugin for translations.
    Everything works great except for the field instructions on frontend.
    Filling the instructions field with string like
    [:it]istruzioni[:en]instructions[:fr]directives[:]
    displays instructions in the right way on back-end but it doesn’t work the same in front-end forms.
    After the uncomplete (no instructions where will be to be displayed) form was sent all the things become correct.
    I discover it seems to be related to qtranslate tags (short or long doesn’t matter) because if I leave simple text instead of multilanguage string everything goes well.

    I’m using this filter:

    function acf_translate_fields($field) {
        if (!is_admin()) { //check if is front-end
            $field['label'] = qtrans_use(qtrans_getLanguage(), $field['label'], false);
            if ($field['instructions']) { //check if field instructions is empty
                $field['instructions'] = qtrans_use(qtrans_getLanguage(), $field['instructions'], false);
                var_dump($field['instructions']); //this return an empty string if instructions was filled with qtranslate tags
            }
        } else { // display translated field in backend
            global $post_type;
            if ($post_type == 'my_cpt') { // display translations only for my_cpt post type
                $field['label'] = qtrans_use(qtrans_getLanguage(), $field['label'], false);
                $field['instructions'] = qtrans_use(qtrans_getLanguage(), $field['instructions'], false);
            }
        }
        return $field;
    }
    
    add_filter('acf/load_field', 'acf_translate_fields');

    Can someone give me some directions to get it work properly?
    Thank in advance

  • If your still looking for help with this.

    What is displayed on the front end for the instructions if you do not try to translate it? Do you see the string that’s supposed to be given to qtranslate? Is anything extra added, tags converted, for example: <' to '<, etc?

  • Hi,
    If I write instructions without qtranslate tags (a simple normal text in one language) on front end it looks perfect.
    The string which I’ve insert doesn’t have extra or converted chars.
    so writing:
    (name and number of street)
    works as expected.
    instead writing:
    [:it](nome e numero della strada)[:en](name and number of street)[:fr](nom et numéro de la rue)[:]
    won’t works.
    I think is breaking something inside field rendering because I get this result when I do a print_r of the field:

    Array
    (
        [ID] => 484
        [key] => field_556d8c8ddc6bc
        [label] => indirizzo
        [name] => indirizzo
        [prefix] => 
        [type] => text
        [value] => 
        [menu_order] => 5
        [instructions] => 
        [required] => 0
        [id] => 
        [class] => 
        [conditional_logic] => 0
        [parent] => 469
        [wrapper] => Array
            (
                [width] => 
                [class] => 
                [id] => 
            )
    
        [_name] => indirizzo
        [_input] => 
        [_valid] => 1
        [default_value] => 
        [maxlength] => 
        [placeholder] => 
        [prepend] => 
        [append] => 
        [readonly] => 0
        [disabled] => 0
    )
    

    but $field['instructions'] is not empty! It’s filled with:
    [:it](nome e numero della strada)[:en](name and number of street)[:fr](nom et numéro de la rue)[:] and also $field['wrapper'] settings are lost

    The really strange behavior is that:
    if form is submitted (so after a POST request) it works as expected.

    Array
    (
        [ID] => 484
        [key] => field_556d8c8ddc6bc
        [label] => indirizzo
        [name] => indirizzo
        [prefix] => 
        [type] => text
        [value] => 
        [menu_order] => 5
        [instructions] => (nome e numero della strada)
        [required] => 1
        [id] => 
        [class] => 
        [conditional_logic] => 0
        [parent] => 469
        [wrapper] => Array
            (
                [width] => 50
                [class] => 
                [id] => 
            )
    
        [_name] => indirizzo
        [_input] => 
        [_valid] => 1
        [default_value] => 
        [placeholder] => 
        [prepend] => 
        [append] => 
        [maxlength] => 
        [readonly] => 0
        [disabled] => 0
    )
  • Honestly, I don’t know what’s going on either, basically I’m stabbing in the dark.

    I have some more question though.

    The first print_r() results you posted. Is that before or after the attempt to translate?

    You said that before the post is submitted it’s not working but after it is working. What happens if you edit the post in the admin and save it and then load that post in the front end? Is it still not working?

  • The first print_r() results I posted is after the attempt to translate.
    if I edit the post in the admin and save it and then load that post in the front end it is still not working.
    It start to work only after form submission.
    I discover that the problem is in api-filed.php in function _acf_get_field_by_id (starts around line 916).

    First time the page with the form is loaded

    // get post
    	$post = get_post( $post_id );

    return translated values instead of full string values in this way:

    WP_Post Object
    (
        [ID] => 655
        [post_author] => 1
        [post_date] => 2015-06-17 12:25:15
        [post_date_gmt] => 2015-06-17 10:25:15
        [post_content] => a:12:{s:4:"type";s:4:"text";s:12:"instructions";s:99:"(nome e numero della strada)";s:8:"required";i:1;s:17:"conditional_logic";i:0;s:7:"wrapper";a:3:{s:5:"width";i:50;s:5:"class";s:0:"";s:2:"id";s:0:"";}s:13:"default_value";s:3:"azz";s:11:"placeholder";s:0:"";s:7:"prepend";s:0:"";s:6:"append";s:0:"";s:9:"maxlength";s:0:"";s:8:"readonly";i:0;s:8:"disabled";i:0;}
        [post_title] => indirizzo
        ........
    )

    instead of the correct way which is this one:

    WP_Post Object
    (
        [ID] => 655
        [post_author] => 1
        [post_date] => 2015-06-17 12:25:15
        [post_date_gmt] => 2015-06-17 10:25:15
        [post_content] => a:12:{s:4:"type";s:4:"text";s:12:"instructions";s:99:"[:it](nome e numero della strada)[:en](name and number of street)[:fr](nom et numéro de la rue)[:]";s:8:"required";i:1;s:17:"conditional_logic";i:0;s:7:"wrapper";a:3:{s:5:"width";i:50;s:5:"class";s:0:"";s:2:"id";s:0:"";}s:13:"default_value";s:3:"azz";s:11:"placeholder";s:0:"";s:7:"prepend";s:0:"";s:6:"append";s:0:"";s:9:"maxlength";s:0:"";s:8:"readonly";i:0;s:8:"disabled";i:0;}
        [post_title] => [:it]indirizzo[:en]address[:fr]adresse[:]
        ........
    )

    and which is the way $post object is correctly returned after form submission

    In that way inside the _acf_get_field_by_id function

    // unserialize
    	$data = maybe_unserialize( $post->post_content );

    returns a boolean instead of an array due unserialization which gets error while parsing the uncorrect
    s:99:"(nome e numero della strada)";

    of course: the string length is 28 chars and not 99!

  • I got it!!!
    simply add this line at the beginning of _acf_get_field_by_id function in api-field.php

    remove_all_filters('the_posts');

    All the instructions are now perfectly translated in front-end!

  • @puntonero that works!
    is there a “clean” way to do it with a filter?
    I would love to avoid to edit api-filed.php file at every update 😉

  • Hi, my solution:
    In the instructions texts I removed the colons of the qtranslate shorttags and add them dynamically with a string replace function in the load field filter followed by the apply_filters function to get the translation.

    [en] English text [de] German text []

    function qtrans_acf_load_field(  $field )
    {   
        // exclude acf-field-group page
        if(get_post_type() != 'acf-field-group'){
         
            $instructions = str_replace('[', '[:', $field['instructions']);  
            $field['instructions']  =  apply_filters('the_title', $instructions);
    
        }
        return $field;
    }
    add_filter('acf/load_field', 'qtrans_acf_load_field', 10,3);
Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘acf_form field instructions translations’ is closed to new replies.