• Skip to main content
  • What We Do
  • Our Work
  • Contact

Mackey

Advanced Custom Fields (ACF) Options Page and Polylang

August 30, 2017 by Mackey

Advanced Custom Fields (ACF) Options Page and Polylang

We’ve been working recently on a multilingual site for Sonoma County Adult Education and it is important that they have full support for both English and Spanish. Since we’re using WordPress to build the site we opted to use the WordPress plugin Polylang. Polylang has been very simple to setup and configure even with our multiple taxonomies and custom post types and even with Advanced Custom Fields (ACF).

When we build sites we use Advanced Custom Fields often to build out a more custom backend for the content manager and cater it to the client’s needs. This includes usually a “Site Options” page that has various fields (images, text, WYSIWYG, etc) that allow the client to manage more global content. But unlike posts, pages, taxonomies, etc there was no out of the box support for multiple Site Options based on languages. In other words, there was just one single Site Options page and we needed two versions — English and Spanish.

We found a nifty little solution on the ACF forums to integrate ACF Options Page and Polylang, you can see the full post here. But the crux of it is to register multiple ACF Option Pages based off the languages you are supporting with Polylang.

Register ACF Options with Polylang Support

In your functions.php file add the following.

if( function_exists('acf_add_options_page') ) {

  // Language Specific Options
  // Translatable options specific languages. e.g., social profiles links
  // 
  
  $languages = array( 'en', 'es' );

  foreach ( $languages as $lang ) {
    acf_add_options_page( array(
      'page_title' => 'Site Options (' . strtoupper( $lang ) . ')',
      'menu_title' => __('Site Options (' . strtoupper( $lang ) . ')', 'text-domain'),
      'menu_slug'  => "site-options-${lang}",
      'post_id'    => $lang
    ) );
  }

}

What are we doing above? Essentially we are registering an ACF Options Page for each language. Where it reads  $languages = array( 'en', 'es' ); replace en and es with your languages or add additional languages. Change Site Options text to whatever you want your Options page to be named.

Add the_field() to Your Templates

Usually when using the ACF Options page to include the field you would write,the_field('YOUR_FIELD','option'); but instead, we are going to replace ‘option’ with ‘pll_current_language(‘slug’).’ So your final code for including a field would look like  the_field('YOUR_FIELD',pll_current_language('slug')); .

Final Code

the_field('YOUR_FIELD',pll_current_language('slug'));

This works for repeater fields, logic, and pretty much anything you can do normally with ACF when including in the template.

Hopefully, this saves someone some trouble. If you have any questions leave them in the comments.

 

 

 

Filed Under: Code Snippets, Development, Wordpress

Reader Interactions

Comments

  1. Tom Hermans says

    January 10, 2018 at 11:23 am

    Interesting idea. 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

    • Mackey says

      January 10, 2018 at 12:00 pm

      Hi Tom, so on the backend of WP it’s outputting the two Site Options (EN and FR), but when editing one, it is updating the other? Is this happening whether you edit EN or FR options? Can you send your functions code (info@mackeycreativelab.com) or paste here?

      • Tom Hermans says

        April 8, 2018 at 2:07 am

        I sent you the code, but I think I found the culprit. When changing Site Options page
        http://theuma:8888/wp-admin/admin.php?page=site-options-nl-nl&lang=nl-be

        The lang parameter decides which post_id is being updated. Since you don’t change language you’re currently working on, it changes the same page over and over again… When you do change language and switch Site Options page, you see empty fields all of a sudden..

  2. Culea Maxime says

    May 17, 2018 at 10:48 am

    Hi there,

    I’ve read carefully your blogpost, it would work like you said.

    But there is one thing I could say to argue in order to use a droppin solution like https://github.com/BeAPI/acf-options-for-polylang. By the way you are doing it, you should change everywhere else in you code.

    With this kind of plugin, it’s seamless 😉

  3. Sourav Nath says

    December 16, 2020 at 3:16 am

    when i write code the_field(‘YOUR_FIELD’,pll_current_language(‘slug’)); in template then it will show an error

Mackey Creative Lab
Web Design and Development, Branding, Graphic Design and Marketing Strategy
680 East Colorado Blvd., Suite 180 & Second Floor, Pasadena, CA 91101
info@mackeycreativelab.com // 626 214 5093
© 2014-2021 Mackey, LLC, All Rights Reserved

Want to join the Lab? Become a lab partner

Need Wordpress Performance & Security Management? Learn More