Support

Account

Home Forums General Issues Options page + polylang

Solved

Options page + polylang

  • ACF works great with Polylang. Everything works out of the box as expected. No dirty hacks are needed… except options page. Is there any way to make it translatable?

    Now I use a workaround, I duplicate fields groups and add prefixes to all the fields. But I don’t like this solution. Is there a better way?

    I could possibly provide a custom post_id value when registering an options page and save options page values in a regular post. But It’s still a hacky solution because it’s database dependant.

  • OK, I mark it solved. I’ve just found that options page doesn’t require an ID of existing page. I can use any string as a post_id.

  • So my solution is to register as many options pages as the number of languages we have. Each options page has a unique page_id:

    foreach (['ru', 'en', 'es', 'de'] as $lang) {
    
        acf_add_options_sub_page([
            'page_title' => "Page name ${$lang}",
            'menu_title' => __("Page name ${$lang}", 'textdomain'),
            'menu_slug' => "page-name-${lang}",
            'post_id' => $lang,
            'parent' => 'parent-slug'
        ]);
    
    }

    Then there’s a need to create a field group and duplicate it as many times as the number of options pages we have registered. Each options page has it’s own ID so fields names can be the same.

    Now we can simply do:
    the_field('my_field_name', 'en');

    or even:
    the_field('my_field_name', pll_current_language('slug'));

  • Thanks wube, creating page-specific option subpages has helped me a lot.
    However, I’m not sure as to why you duplicate your field groups โ€“ why not simply assign the same field group to the various option pages?

  • why not simply assign the same field group to the various option pages?

    You’re right. Thanks for a tip. I have no idea why I did it this way ๐Ÿ™‚

  • Another way of dealing with this core-unsupported feature is to use the following plugin: https://github.com/BeAPI/acf-options-for-polylang

    I’ve just tried it out and it allows you to have the same option-page for different languages. You can edit them by changing the language in the admin-top-menubar.

  • Thanks @wube your solution worked great for me. I’ve expanded it a little bit more to have these option pages:

    – Global Options (options same for all languages, e.g., social profile links)
    – Options (EN) (options specific to English language)
    – Options (IT) (options specific to Italian language)

    /**
     * ACF Options Page
     */
    
    if ( function_exists( 'acf_add_options_page' ) ) {
    
      
      // Main Theme Settings Page
      $parent = acf_add_options_page( array(
        'page_title' => 'Theme General Settings',
        'menu_title' => 'Theme Settings',
        'redirect'   => 'Theme Settings',
      ) );
    
      // 
      // Global Options
      // Same options on all languages. e.g., social profiles links
      // 
    
      acf_add_options_sub_page( array(
        'page_title' => 'Global Options',
        'menu_title' => __('Global Options', 'text-domain'),
        'menu_slug'  => "acf-options",
        'parent'     => $parent['menu_slug']
      ) );
    
      // 
      // Language Specific Options
      // Translatable options specific languages. e.g., social profiles links
      // 
      
      $languages = array( 'it', 'en' );
    
      foreach ( $languages as $lang ) {
        acf_add_options_sub_page( array(
          'page_title' => 'Options (' . strtoupper( $lang ) . ')',
          'menu_title' => __('Options (' . strtoupper( $lang ) . ')', 'text-domain'),
          'menu_slug'  => "options-${lang}",
          'post_id'    => $lang,
          'parent'     => $parent['menu_slug']
        ) );
      }
    
    }
    

    Gist: https://gist.github.com/zeshanshani/60bdf497bcae87bd3a8b03920cf64fc6

    And just like @philby mentioned, you can set an option group to show on all language specific option pages.

    Hope it helps someone looking for the complete solution. ๐Ÿ™‚


    @alchemistics
    I tried the plugin, it indeed works, but a bit buggy as in initial stages I assume. For example, when you select to show all languages, the options page is empty again so could confusing for normal users.

  • I tried it, set the locations rule to the different lang-country options I made available via the snippet (โ€˜ORโ€™ rule), but if I change a value in e.g. โ€˜Site Options ENโ€™, the value also changes in โ€˜Site Options FR-FRโ€™, that canโ€™t be right I think ? What am I missing ??

  • Hi there the ACF community ๐Ÿ™‚
    We did https://github.com/BeAPI/acf-options-for-polylang plugin to support this core-unsupported feature.
    If you have some issues, inquirements, improvements or requests please refer to the github. We will be glad to help you.

    @tomhermans
    , @zeshanshani note that we recently updated the plugin, so maybe your issue has been fixed. Please check it out!
    Many thanks

  • I used the plugin https://github.com/BeAPI/acf-options-for-polylang and it worked very nice for me.
    For people how wants to remove the confusing ‘all languages options page’, they can use these filters:

    add_filter( 'bea.aofp.get_default', '__return_false' );
    In order to remove the fallback feature for this generic option page if the field in the option page for a certain language is empty.

    add_filter( 'pll_admin_languages_filter', function ( $adminBarLanguages ) {
        global $pagenow;
        if ( $pagenow === 'admin.php' && isset( $_GET['page'] ) && $_GET['page'] === 'acf-options' ) {
            unset( $adminBarLanguages[0] );
        }
    
        return $adminBarLanguages;
    } );

    In order to remove the all language entry fron the admin dropdown menu.

    * Feel free to respond to me if something seems wrong with my workaround ๐Ÿ™‚

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

The topic ‘Options page + polylang’ is closed to new replies.