Skip to content

Instantly share code, notes, and snippets.

@xeiter
Last active September 3, 2016 04:56
Show Gist options
  • Save xeiter/f177bfdbe8df40593bfa31346845b20b to your computer and use it in GitHub Desktop.
Save xeiter/f177bfdbe8df40593bfa31346845b20b to your computer and use it in GitHub Desktop.
Control the display of ACF groups programmatically in WordPress
<?php
add_filter('acf/load_field/name=name', 'az_acf_meta_box_display');
add_filter('acf/load_field/name=age', 'az_acf_meta_box_display');
add_filter('acf/load_field/name=favorite_colour', 'az_acf_meta_box_display');
/**
* Control the display of ACF groups programmatically in WordPress
*
* @param array $field
* @return mixed
*/
function az_acf_meta_box_display( $field ) {
// Get a global that contains information about current screen in WordPress admin
global $current_screen;
// WordPress option "az_some_plugin_settings" contains a setting for each of the post types to control the display
// of the ACF fields when editing an entry of that post type
// az_some_plugin_settings['page'] = true
// az_some_plugin_settings['post'] = true
// az_some_plugin_settings['some-other-cpt'] = true
$az_option_that_controls_display = get_option('az_some_plugin_settings');
// Perform a check for each of the post type and show or display an ACF fields group
if ( !$az_option_that_controls_display || !isset( $az_option_that_controls_display[ 'cpt_selection_' . $current_screen->post_type ] ) || $az_option_that_controls_display[ 'cpt_selection_' . $current_screen->post_type ] != '1' ) {
$field['wrapper']['class'] = 'hidden';
}
return $field;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment