Last active
September 3, 2016 04:56
-
-
Save xeiter/f177bfdbe8df40593bfa31346845b20b to your computer and use it in GitHub Desktop.
Control the display of ACF groups programmatically in WordPress
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=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