Skip to content

Instantly share code, notes, and snippets.

@jamiemitchell
Forked from solepixel/acf-set-field-name.php
Created January 24, 2018 00:41
Show Gist options
  • Select an option

  • Save jamiemitchell/a91b22226fb69a451eadcb6177738e0f to your computer and use it in GitHub Desktop.

Select an option

Save jamiemitchell/a91b22226fb69a451eadcb6177738e0f to your computer and use it in GitHub Desktop.
Differentiate Settings Page fields when using the same field groups. Requires "menu_slug" prefixed with something common and identifiable, in this case the Post Type of the Archive as well as a common naming convention, in this example it uses "-archive-settings".
<?php
add_filter( 'acf/load_field', 'mytheme_set_field_name' );
function mytheme_set_field_name( $field ){
if( is_admin() ){
$screen = get_current_screen();
if( $screen->id == 'acf-field-group' )
return $field;
$admin_screen = '-archive-settings';
if( strpos( $screen->id, $admin_screen ) === false )
return $field;
$page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
$post_type = str_replace( $admin_screen, '', $page );
} else {
$post_type = get_post_type();
}
$field['name'] = '_' . $post_type . $field['name'];
$field['_name'] = $field['name'];
return $field;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment