Skip to content

Instantly share code, notes, and snippets.

@hereswhatidid
Last active April 22, 2019 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hereswhatidid/c70dcc83efb66bd61c8b61b5c25b7003 to your computer and use it in GitHub Desktop.
Save hereswhatidid/c70dcc83efb66bd61c8b61b5c25b7003 to your computer and use it in GitHub Desktop.
Set correct language for ACF options pages with custom post_id
<?php
class ACF_SampleOptions_Page {
static $slug = 'sample_options_slug';
static $post_id = 'sample_options_id';
public static function init() {
// This sets the options page `post_id` to the correct value based on the current language
if ( defined( 'ICL_LANGUAGE_CODE' ) && ICL_LANGUAGE_CODE !== 'en' ) {
self::$post_id .= '_' . ICL_LANGUAGE_CODE;
}
if ( function_exists( 'acf_add_local_field_group' ) ) {
acf_add_local_field_group( array(
'key' => self::$slug,
'title' => __( 'Sample Options', 'rmg' ),
'fields' => array(
[
'key' => self::$slug . 'samplefield1',
'label' => __( 'Basic Text Field', 'textdomain' ),
'name' => 'sample_field_1',
'type' => 'text',
'instructions' => '',
],
[
'key' => self::$slug . 'samplefield2',
'label' => __( 'Basic Text Field', 'textdomain' ),
'name' => 'sample_field_2',
'type' => 'text',
'instructions' => '',
],
[
'key' => self::$slug . 'samplefield3',
'label' => __( 'Basic Text Field', 'textdomain' ),
'name' => 'sample_field_3',
'type' => 'text',
'instructions' => '',
],
),
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => self::$slug,
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array(
'position' => 'normal',
'layout' => 'default',
'hide_on_screen' => array(),
),
'menu_order' => 0,
) );
}
if ( function_exists( 'acf_add_options_sub_page' ) ) {
acf_add_options_sub_page( array(
'menu_title' => 'Sample Options',
'menu_slug' => self::$slug,
'post_id' => self::$post_id,
'parent_slug' => 'edit.php?post_type=page'
) );
}
}
}
add_action( 'acf/init', [ 'ACF_SampleOptions_Page', 'init' ] );
// To retrieve the value from ACF you can use a call like this:
$field_value = get_field( 'sample_field_1', ACF_SampleOptions_Page::$post_id );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment