ACF_Form_Post::add_meta_boxes
add_meta_boxes
Adds ACF metaboxes for the given $post_type and $post.
Method of the class: ACF_Form_Post{}
Hooks from the method
Returns
null. Nothing (null).
Usage
$ACF_Form_Post = new ACF_Form_Post(); $ACF_Form_Post->add_meta_boxes( $post_type, $post );
- $post_type(string) (required)
- The post type.
- $post(WP_Post) (required)
- The post being edited.
Changelog
| Since 5.7.6 | Introduced. |
ACF_Form_Post::add_meta_boxes() ACF Form Post::add meta boxes code ACF 6.8.8
function add_meta_boxes( $post_type, $post ) {
// Storage for localized postboxes.
$postboxes = array();
// Get field groups for this screen.
$field_groups = acf_get_field_groups(
array(
'post_id' => $post->ID,
'post_type' => $post_type,
)
);
// Loop over field groups.
if ( $field_groups ) {
foreach ( $field_groups as $field_group ) {
$id = esc_attr( "acf-{$field_group['key']}" );
$context = esc_attr( $field_group['position'] );
$priority = 'high';
// Reduce priority for sidebar metaboxes for best position.
if ( $context === 'side' ) {
$priority = 'core';
}
/**
* Filters the metabox priority.
*
* @date 23/06/12
* @since 3.1.8
*
* @param string $priority The metabox priority (high, core, default, low).
* @param array $field_group The field group array.
*/
$priority = apply_filters( 'acf/input/meta_box_priority', $priority, $field_group );
// Localize data
$postboxes[] = array(
'id' => $id,
'key' => esc_attr( $field_group['key'] ),
'style' => esc_attr( $field_group['style'] ),
'label' => esc_attr( $field_group['label_placement'] ),
'edit' => esc_url( acf_get_field_group_edit_link( $field_group['ID'] ) ),
);
// Add the meta box.
add_meta_box(
$id,
acf_esc_html( acf_get_field_group_title( $field_group ) ),
array( $this, 'render_meta_box' ),
$post_type,
$context,
$priority,
array( 'field_group' => $field_group )
);
}
// Set style from first field group.
$this->style = acf_get_field_group_style( $field_groups[0] );
// Localize postboxes.
acf_localize_data(
array(
'postboxes' => $postboxes,
)
);
}
// remove postcustom metabox (removes expensive SQL query)
if ( acf_get_setting( 'remove_wp_meta_box' ) ) {
remove_meta_box( 'postcustom', false, 'normal' );
}
// Add hidden input fields.
add_action( 'edit_form_after_title', array( $this, 'edit_form_after_title' ) );
/**
* Fires after metaboxes have been added.
*
* @date 13/12/18
* @since 5.8.0
*
* @param string $post_type The post type.
* @param WP_Post $post The post being edited.
* @param array $field_groups The field groups added.
*/
do_action( 'acf/add_meta_boxes', $post_type, $post, $field_groups );
}