acf_admin_field_group::save_post
This function will save all the field group data
@since 1.0.0
@param int $post_id The post ID. @param WP_Post $post The post object.
@return $post_id (int)
Method of the class: acf_admin_field_group{}
No Hooks.
Returns
null. Nothing (null).
Usage
$acf_admin_field_group = new acf_admin_field_group(); $acf_admin_field_group->save_post( $post_id, $post );
- $post_id(required)
- .
- $post(required)
- .
acf_admin_field_group::save_post() acf admin field group::save post code ACF 6.0.4
public function save_post( $post_id, $post ) {
// do not save if this is an auto save routine.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// bail early if not acf-field-group.
if ( $post->post_type !== 'acf-field-group' ) {
return $post_id;
}
// only save once! WordPress save's a revision as well.
if ( wp_is_post_revision( $post_id ) ) {
return $post_id;
}
// verify nonce.
if ( ! acf_verify_nonce( 'field_group' ) ) {
return $post_id;
}
// Bail early if request came from an unauthorised user.
if ( ! current_user_can( acf_get_setting( 'capability' ) ) ) {
return $post_id;
}
// disable filters to ensure ACF loads raw data from DB.
acf_disable_filters();
// save fields.
if ( ! empty( $_POST['acf_fields'] ) ) {
// loop.
foreach ( $_POST['acf_fields'] as $field ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized when saved.
if ( ! isset( $field['key'] ) ) {
continue;
}
// vars.
$specific = false;
$save = acf_extract_var( $field, 'save' );
// only saved field if has changed.
if ( $save == 'meta' ) {
$specific = array(
'menu_order',
'post_parent',
);
}
// set parent.
if ( ! $field['parent'] ) {
$field['parent'] = $post_id;
}
// save field.
acf_update_field( $field, $specific );
}
}
// delete fields.
if ( $_POST['_acf_delete_fields'] ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below.
// clean.
$ids = explode( '|', $_POST['_acf_delete_fields'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below.
$ids = array_map( 'intval', $ids );
// loop.
foreach ( $ids as $id ) {
// bai early if no id.
if ( ! $id ) {
continue;
}
// delete.
acf_delete_field( $id );
}
}
// add args.
$_POST['acf_field_group']['ID'] = $post_id;
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized when saved.
$_POST['acf_field_group']['title'] = $_POST['post_title'];
// save field group.
acf_update_field_group( $_POST['acf_field_group'] );
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
return $post_id;
}