Last active
January 30, 2023 08:41
-
-
Save yratof/9abc805a64c1504ec2b282aaa9a3beaa to your computer and use it in GitHub Desktop.
ACF Load layouts into flex field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'acf/load_field/name=flex_layout', __CLASS__ . '::craft_content_layouts' ); | |
static function craft_content_layouts( $field ) { | |
// Remove the layouts | |
// that are named in this list | |
$remove_list = [ | |
'paragraph', | |
'banner', | |
]; | |
foreach ( $field['layouts'] as $i => $layout ) { | |
if ( in_array( $layout['name'], $remove_list ) ) { | |
unset( $field['layouts'][ $i ] ); | |
} | |
} | |
/** | |
* Load another AC Flex field into this layout mix | |
*/ | |
// $additional_fields = acf_get_fields( 'additional_flex_field' ); | |
// foreach ($additional_fields['layouts'] as $layout) { | |
// $field['layouts'][] = $layout; | |
// } | |
return $field; | |
} ); |
Due I wasn't able to load a single field using acf_get_fields but it was loading acf groups, I fixed loading additional layouts using a previously created acf group and it's working:
add_filter( 'acf/load_field/key=field_60f57a54a887b', 'myfuncaddlayout' );
function myfuncaddlayout( $field ) {
if(array_search('mycustomfieldname', array_column($field['layouts'], 'name')) === false){
$additional_fields = acf_get_fields( 'group_63d777aa87ad7' );
foreach ($additional_fields as $newfield) {
foreach ($newfield['layouts'] as $layout) {
$field['layouts'][] = $layout;
}
}
}
return $field;
} );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! it is working properly.