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.
Tom Hermans says
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
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
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..
Culea Maxime says
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 😉
Sourav Nath says
when i write code the_field(‘YOUR_FIELD’,pll_current_language(‘slug’)); in template then it will show an error