Search

How to add ACF Options Pages to Polylang websites

Recently I was required to add an editing option to an Archive Page on a website that works with Polylang. Since it’s not possible to add fields to an archive page using ACF, I had to add the editing option through adding an Options Page in ACF.

Archive pages are “virtual pages” and doesn’t exist under Pages in the the WordPress dashboard.

However, the challenge was to make that Options Page appear in multiple languages since the website is multilingual. But how can we add the options page for each of the languages defined in Polylang? Furthermore, how can we retrieve the information we enter in those fields in our template for each language?

Let’s see how to do it…

Adding an ACF Options Page to a Polylang Site

To add the same Options Page for each of the languages defined in Polylang, add the following code to the functions.php file:

if (function_exists('acf_add_options_page')) {
    // Main Theme Settings Page
    acf_add_options_page();

    $parent = acf_add_options_page(array(
        'page_title' => 'Archive General Settings',
        'menu_title' => 'Archive Settings',
        'redirect' => 'Archive Settings',
    ));
    
    $languages = array('cn', 'en');
    
    // 
    // Language Specific Options
    // Translatable options specific languages. e.g., social profiles links
    // 
    
    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']
        ));
    }
}

Please note that you need to define the languages defined in Polylang on line 11 by adding the language codes of each language. You can find the language code in Polylang under Languages > Languages in the WordPress dashboard.

Polylang Language Settings - Code

In the case of the site I worked on, it’s English and Chinese, and these are specified on line 11 in the code above. After adding the code, you will see the following options in the WordPress admin interface:

Polylang Archive Settings

At this point, you need to add the desired fields to those pages when creating a new field group under Location.

Field Location in Options Page

With this, you have completed this part. After adding the fields, on the site I manage, the options pages will look like this:

Populated Fields - ACF Options

Now, let’s understand how to retrieve these fields in our template according to the language of the page we are viewing…

Fetching Fields of the Options Page Based on the Relevant Language

This part is simpler.

To get the data in PHP of those fields we created, based on the language of the page we are viewing, we need to use the pll_current_language function as the second parameter in calling that field, whether it’s using the get_field function that you should be familiar with or any other ACF function that retrieves data.

Here are two examples:

<?php $image = get_field('image', pll_current_language('slug')); ?>

Or, for instance, when calling a Repeater Field:

<?php if (have_rows('my_repeater_field', pll_current_language('slug'))) : ?>
    <?php while (have_rows('my_repeater_field', pll_current_language('slug'))) : the_row(); ?>

That’s it.

Roee Yossef
Roee Yossef

I develop websites & custom WordPress themes by design. I love typography, colors & everything between, and aim to provide high performance, seo optimized websites with a clean & semantic code.

0 Comments...

Leave a Comment

Up!
Blog