Support

Account

Home Forums Add-ons Options Page Don't translate option page

Solving

Don't translate option page

  • I create an option page with ACF PRO and i want that fields are avaiable in all languages.

    Now i get to them with

    get_field(‘name’, ‘options’)

    but they arent’ displayed in translated pages.

    Ty

  • Hi @effebiconsulting

    You can make use of the acf/settings filter to reset the current language so as to return language independent theme options. The code would look like so:

    // add this
    add_filter('acf/settings/current_language',function() {
         global $sitepress;
         return $sitepress->get_default_language();
    });
    
    // this is already in your code
    $my_field =  get_field('my_field', 'option');
    
    // reset to original language
    add_filter('acf/settings/current_language',function() {
         return ICL_LANGUAGE_CODE;
    });
  • Hi @james,

    that code should be stored in functions.php or should be every file that we use elements from options.

    Thanks,
    Andreas

  • A little addition, although it’s an older topic.

    I needed this as well, but as @andreasdm mentioned already, it needs to added ‘around’ each option that you retrieve.

    So I made it a bit easier, so you don’t have to add this around each get option field you use. I made a custom function (which needs to be added in functions.php) which returns the original value, based on the field name that is inputted.

    /**
     * Force return default language option value
     *
     * @param bool $field_name
     *
     * @return bool|mixed|null
     */
    function beee_get_filtered_option( $field_name = false ) {
    
        if ( false == $field_name ) {
            return $field_name;
        }
    
        /**
         * Set language to default
         *
         * @return bool|mixed
         */
        function beee_set_default_lang() {
            global $sitepress;
            return $sitepress->get_default_language();
        }
        add_filter( 'acf/settings/current_language', 'beee_set_default_lang' );
    
        // retrieve $field_name option value
        $value = get_field( $field_name, 'option' );
    
        /**
         * Reset to 'real' language
         *
         * @return mixed
         */
        function beee_reset_to_real_lang() {
            return ICL_LANGUAGE_CODE;
        }
        add_filter( 'acf/settings/current_language', 'beee_reset_to_real_lang' );
    
        return $value;
    }
    if ( class_exists( 'acf' ) ) {
        add_action( 'init', 'sd_get_filtered_option' );
    }

    Add this as is. No changes needed.

    Then call each option value like this:
    beee_get_filtered_option( $option_field_name );

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

You must be logged in to reply to this topic.