Skip to content

Instantly share code, notes, and snippets.

@zeshanshani
Last active October 10, 2023 11:25
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zeshanshani/60bdf497bcae87bd3a8b03920cf64fc6 to your computer and use it in GitHub Desktop.
Save zeshanshani/60bdf497bcae87bd3a8b03920cf64fc6 to your computer and use it in GitHub Desktop.
Creates separate Advanced Custom Fields options pages for the specified languages.
<?php // Don't copy this line if you are inserting in a file that has it already.
/**
* ACF Options Page
*
* Instructions:
* Add more languages to the array below: $languages
*
* @author: Zeshan Ahmed <https://zeshanahmed.com/>
*/
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.
//
$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']
) );
}
}
@wellya
Copy link

wellya commented Mar 12, 2020

if you don’t want to manually add or remove languages.
Just replace - $languages = array( 'it', 'en' ); 39 string with -

pll_the_languages(array('raw' => 1));
$languages = pll_languages_list();

This is relevant for Polylang plugin

@scesini
Copy link

scesini commented Jan 30, 2023

Some ways to replace the:
$languages = array( 'it', 'en' );
to automatically add the WPML languages?

@michal-wajrak
Copy link

michal-wajrak commented Oct 10, 2023

@scesini According to the Polylang documentation, you can use the pll_the_languages function to retrieve all available languages.

Replace:

$languages = array( 'it', 'en' );

with:

$polylang_languages = pll_the_languages(['raw' => 1]);
$languages = array_column($polylang_languages, 'slug');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment