Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daggerhart/5f979af8467deeff88873e391d80f0ab to your computer and use it in GitHub Desktop.
Save daggerhart/5f979af8467deeff88873e391d80f0ab to your computer and use it in GitHub Desktop.
Patch to Advanced Custom Fields 5.5.10 that allows changes to fields in the UI to affect output even if field is "local" JSON or PHP
diff --git a/admin/views/field-group-options.php b/admin/views/field-group-options.php
index ae57dfa..c0871b8 100755
--- a/admin/views/field-group-options.php
+++ b/admin/views/field-group-options.php
@@ -127,6 +127,18 @@ acf_render_field_wrap(array(
));
+// db overrides local
+acf_render_field_wrap(array(
+ 'label' => __('Override local on display','acf'),
+ 'instructions' => __('Settings in this field group override the exported json or php on render.','acf'),
+ 'type' => 'true_false',
+ 'name' => 'db_overrides_local_output',
+ 'prefix' => 'acf_field_group',
+ 'value' => $field_group['db_overrides_local_output'],
+ 'ui' => 1,
+));
+
+
// 3rd party settings
do_action('acf/render_field_group_settings', $field_group);
diff --git a/api/api-field-group.php b/api/api-field-group.php
index af39f06..91c1145 100755
--- a/api/api-field-group.php
+++ b/api/api-field-group.php
@@ -407,7 +407,7 @@ function _acf_get_field_group_by_id( $post_id = 0 ) {
// override with JSON
- if( acf_is_local_field_group( $field_group['key'] ) ) {
+ if( acf_local_overrides_db( $post_id ) && acf_is_local_field_group( $field_group['key'] ) ) {
// load JSON field
$local = acf_get_local_field_group( $field_group['key'] );
@@ -443,19 +443,19 @@ function _acf_get_field_group_by_id( $post_id = 0 ) {
*/
function _acf_get_field_group_by_key( $key = '' ) {
-
+
+ // vars
+ $post_id = acf_get_field_group_id( $key );
+
+
// try JSON before DB to save query time
- if( acf_is_local_field_group( $key ) ) {
+ if( acf_local_overrides_db( $post_id ) && acf_is_local_field_group( $key ) ) {
return acf_get_local_field_group( $key );
}
- // vars
- $post_id = acf_get_field_group_id( $key );
-
-
// bail early if no post_id
if( !$post_id ) return false;
diff --git a/api/api-field.php b/api/api-field.php
index 8eb9031..cc342a7 100755
--- a/api/api-field.php
+++ b/api/api-field.php
@@ -649,7 +649,7 @@ function acf_get_fields( $parent = false ) {
// try JSON before DB to save query time
- if( acf_have_local_fields( $parent['key'] ) ) {
+ if( acf_local_overrides_db( $parent['ID'] ) && acf_have_local_fields( $parent['key'] ) ) {
$fields = acf_get_local_fields( $parent['key'] );
@@ -900,7 +900,7 @@ function _acf_get_field_by_id( $post_id = 0, $db_only = false ) {
// override with JSON
- if( !$db_only && acf_is_local_field($field['key']) ) {
+ if( acf_local_overrides_db( $post_id ) && !$db_only && acf_is_local_field($field['key']) ) {
// load JSON field
$local = acf_get_local_field( $field['key'] );
@@ -938,18 +938,18 @@ function _acf_get_field_by_id( $post_id = 0, $db_only = false ) {
function _acf_get_field_by_key( $key = '', $db_only = false ) {
+ // vars
+ $post_id = acf_get_field_id( $key );
+
+
// try JSON before DB to save query time
- if( !$db_only && acf_is_local_field( $key ) ) {
+ if( acf_local_overrides_db( $post_id ) && !$db_only && acf_is_local_field( $key ) ) {
return acf_get_local_field( $key );
}
- // vars
- $post_id = acf_get_field_id( $key );
-
-
// bail early if no post_id
if( !$post_id ) return false;
@@ -975,14 +975,6 @@ function _acf_get_field_by_key( $key = '', $db_only = false ) {
function _acf_get_field_by_name( $name = '', $db_only = false ) {
- // try JSON before DB to save query time
- if( !$db_only && acf_is_local_field( $name ) ) {
-
- return acf_get_local_field( $name );
-
- }
-
-
// vars
$args = array(
'posts_per_page' => 1,
@@ -996,6 +988,14 @@ function _acf_get_field_by_name( $name = '', $db_only = false ) {
// load posts
$posts = get_posts( $args );
+
+
+ // try JSON before DB to save query time
+ if( !empty($posts) && acf_local_overrides_db( $posts[0]->ID ) && !$db_only && acf_is_local_field( $name ) ) {
+
+ return acf_get_local_field( $name );
+
+ }
// bail early if no posts
diff --git a/core/local.php b/core/local.php
index 1f748f6..cf3a1f0 100755
--- a/core/local.php
+++ b/core/local.php
@@ -973,5 +973,30 @@ function register_field_group( $field_group ) {
}
+// allow field groups ui to override local
+function acf_local_overrides_db( $post_id ){
+
+ // if there is no post_id, use the local
+ if ( ! $post_id ){
+ return true;
+ }
+
+ // default to true as that was the expected result before this change
+ $local_overrides_db = true;
+
+ $field_group_id = $post_id;
+
+ if ( get_post_type( $post_id ) == 'acf-field' ){
+ $field_group_id = get_post_field( 'post_parent', $post_id );
+ }
+
+ $field_group = maybe_unserialize( get_post_field('post_content', $field_group_id, 'raw') );
+
+ if ( isset( $field_group['db_overrides_local_output'] ) ) {
+ $local_overrides_db = ! $field_group['db_overrides_local_output'];
+ }
+
+ return $local_overrides_db;
+}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment