Support

Account

Home Forums ACF PRO WPML, ACF 5.0.9 and Options Page Reply To: WPML, ACF 5.0.9 and Options Page

  • Hi everyone,

    I believe I have a more or less decent looking workaround for this issue, based on the same idea JoepR had. It allows you to mark any number of options pages as “global”, i.e. they are not translatable and will always display the options in the default language. All other options pages would remain translatable.

    
    /**
     * Create the options page that we will later set "global"
     */
    acf_add_options_page(array(
          'page_title'  => 'Test',
          'menu_title'  => 'Test',
          'menu_slug'   => 'test',
    ));
    
    /**
     * Force ACF to use only the default language on some options pages
     */
    function cl_set_global_options_pages($current_screen) {
    
      // IDs of admin options pages that should be "global"
      $page_ids = array(
        "toplevel_page_acf-options-test"
      );
    
      if (in_array($current_screen->id, $page_ids)) {
        add_filter('acf/settings/current_language', 'cl_acf_set_language', 100);
      }
    }
    add_action( 'current_screen', 'cl_set_global_options_pages' );
    
    function cl_acf_set_language() {
      return acf_get_setting('default_language');
    }
    
    /**
     * Wrapper around get_field() to get the "global" option values.
     * This is the function you'll want to use in your templates instead of get_field() for "global" options.
     */
    function get_global_option($name) {
      add_filter('acf/settings/current_language', 'cl_acf_set_language', 100);
      $option = get_field($name, 'option');
      remove_filter('acf/settings/current_language', 'cl_acf_set_language', 100);
      return $option;
    }
    

    I’d appreciate any feedback on the concept. Couldn’t figure out any scenarios where it would break, but maybe I missed something.