Support

Account

Home Forums Add-ons Options Page WPML and ACF Options

Solved

WPML and ACF Options

  • Hi guys.

    I have this legacy code of a WP site that heavily uses ACF and some plugins of ACF (repeater and options).

    However, my customer asked to use WPML, in order to translate in couple of extra languages. While everything looks good on posts and pages (and even custom post types), i can’t figure how should i proceed with options in order to translate them.

    So, any hints on how to takle this?

    Thanks!

  • Hi @iamntz

    The only way for the options page add-on to play nicely with WPML is to create a translation of the field group for each translation.

    Each options page field group will need a prefix for the field names. This prefix is the WPML language code (en_, fr_)

    A field group for english may have a field called en_twitter, en_headline, etc..

    Then, in your template file, you can find the current language code (WPML uses a constant for this, not sure exactly what it is off the top of my head).

    Use this language code to generate the field name: eg:

    
    get_field( LANG_CODE . '_twitter', 'options');
    

    I hope that helps.

    Your other option is to use a custom post type called options. That way you can use WPML as normal. But instead of using ‘options’ as the second param of get_field, you will need to find the post ID of the optiosn post type object

  • Or you can also create a regular page called “translations” where you might put all strings in as acf textfields. Then you’ll be able to translate it as usual pages and it’s a bit simpler than creating a whole cpt for a single page 🙂

    I usually create three pages when dealing with WPML and ACF; Header, Footer and Global translations. These three act as replacement for having three options-pages with ACF. The upside is that they’re all collected in pages for easy access and you don’t need to create a cpt. The downside is that it’s not really semantically correct since they are not really being used as pages.

  • Hmmm, a new page could work. Let’s see!

    Thanks!

  • The correct constant is ICL_LANGUAGE_CODE :

    http://wpml.org/documentation/support/wpml-coding-api/

    Prefixing is not really maintainable because I don’t know which languages our client will add… And can become a mess.

    Creating a “global variables page” is what I was doing before using ACF Options… So I’d rather not regress 😛

    For now I think I will replace “translatable” fields in the Options page by Repeaters ones, in which the key will be the language code.

    It’s a shame that it’s not better handled, is it on your roadmap to enhance this Elliot ? It’s really a pleasure to work on complex and multilingual sites with ACF and WPML, except this part 😉

  • I have this issue – i’m using the Options add on to collect about 30-40 different ads for a WPML Networked blog site (theres 6 sites, but only 2 of them have 2 languages). Our client recently informed us that the french / english sites need to have different ads. I’d rather not have to re program the entire thing, and have to re load all the ad codes for each site, i’m wondering if there is a way to make a separate options page for each available language?

    Is this possible? if so, some guidance would be appreciated!

  • @flashpunk: here is how i did it:

    1) Created a custom post type, named my_settings. Obviously, is translatable by WPML
    2) Created a page template for each settings page. Fortunate, i had only two pages. So, now we have tpl-home_carousel.php and tpl-other_options.php
    3) Created a page for each template. For now it’s enough to have them in base language (english)
    4) Created an option page that sets the id’s for these pages. (there is a post object or something like this; you need the ID) (for the sake of simplicity, i named all options here as wpml_*). We need this only for base language.
    5) The old option pages (those with real … options) are now set as to be displayed on certain templates (those that we set earlier)

    Done with admin!

    Right now, we need a way to get the id for get_field('our_awesome_setting', ID) function.

    We first get the ID for base language page:
    $home_carousel = get_field( 'wpml_options_home_carousel_page', 'option' );

    We then get the translated page (or base page if there is no translation):
    $home_carousel_id = icl_object_id( $home_carousel->ID, 'my_settings', true, ICL_LANGUAGE_CODE );

    Then we set a constant with this translated ID:
    define( 'MY_OPTIONS_HOME_CAROUSEL', $home_carousel_id );

    After this, everything is just fine. We can get fields as usual, but instead of using option param, you use this constant we defined earlier:

    $gallery_rows = get_field( 'home_gallery_rows', MY_OPTIONS_HOME_CAROUSEL );

    Done!

    (let me know if i explained well)

    Very, very late edit: you can set templates to custom post types by using this plugin: http://wordpress.org/plugins/custom-post-type-page-template/

  • Nearly one year later I’d like to know if there are any improvements on this issue?

    I love how all ACF works fine (Repeater, Flexible Content) with WPML but the Options page is not…

    Bart

  • I was waiting also for a solution. Unfortunately time ran out and i had to find another solution.

    I ditched the Options page plugin and used similar plugin called “Types”.
    Although Types plugin serves the same purpose as ACF i still wanted to use it since it has better options (repeater field, etc).

    The problem was that i wanted to use custom fields in footer or elswhere and still have them translated. Second thing was to keep it simple and aesthetic.

    So i made a custom post type with Types- with Title no WYSIWYG editor.
    http://screencast.com/t/3cmHVL6c
    Then made a new page called “Front page banner translation” – and this page can be targeted in ACF. http://screencast.com/t/kHauYS3I
    And all the fields can be translated from there (yey, no more string translation shit)
    http://screencast.com/t/ViCS5EXvVPK

    Only thing you have to do now is to target those different pages and get the content from them. This can be done with this code-
    <?php $nr = icl_object_id( 177, true ); the_field('acf_field_name', $nr); ?>

    …where 177- is the page number of your custom page and acf_field_name your field name (duh).

    Quite simple to the client, easy code-wise.

    Let me know if there are questions

  • 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;
    
  • A little correction for the previous function

    function get_field_wpml( $field_key, $post_id = false, $format_value = true ) {
    
        // see : http://support.advancedcustomfields.com/forums/topic/wpml-and-acf-options/
    
        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
        if ( ( $sitepress->get_default_language() == ICL_LANGUAGE_CODE ) && ( $ret = get_field( $field_key, $post_id, $format_value ) ) ) {
           return $ret;
        }
    
        // get field for current language
        elseif ( $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;
    }
  • I’ve modified ‘Options Page’ Add-on to support WPML natively and in a more transparent manner ; ) I’d love to contribute to this amazing plugin and add-on, so i ask the author to let me know the best way to share the code and distribute the new version of the add-on; can’t see the project on github : (

    Thank’s for all.
    P.

  • If so I would like to have access to that improved code please… could that be possible? Maybe on a private way?

    It would be great!

  • @duepi.biz How can I get this? It will help me a lot!

  • As Elliot said on twitter it’ll be released on next version.

    Heads up! The next version of ACF PRO contains multi-lingual support for options pages! WPML works out of the box + filters for 3rd party 🙂 — elliot condon (@elliotcondon) September 17, 2014

    If you need it before, drop me an email or go get it here in my old fashioned website 😉

    And don’t forget to drop me a line !

    Take care

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

The topic ‘WPML and ACF Options’ is closed to new replies.