Overview
This function is used to process, validate and save the submitted form data created by the acf_form() function. This function will also enqueue all ACF related scripts and styles for the acf form to render correctly. This function must be placed before any HTML has been created, preferably above the get_header();
function in your theme file.
Parameters
<?php acf_form_head(); ?>
Examples
This example shows a basic acf_form()
to edit the current post.
<?php acf_form_head(); ?>
<?php get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php acf_form(); ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Actions & Filters
This function contains actions & filters to customize the saving process.
acf/pre_submit_form
This filter is run when a form has been submit and is valid. Use this filter to modify or use the $form
before $_POST data is processed. Added in version 5.5.10
function my_acf_pre_submit_form( $form ) {
// create post using $form['new_post']
// modify $form['redirect']
// return
return $form;
}
add_filter('acf/pre_submit_form', 'my_acf_pre_submit_form', 10, 1);
acf/pre_save_post
This filter is run when a form has been submit and is value. This filter is run after ‘acf/pre_submit_form’. Use this filter to modify or use the $post_id
and $form
before $_POST data is processed. Added in version 4.1.6
function my_acf_pre_save_post( $post_id, $form ) {
// create post using $form and update $post_id
// return
return $post_id;
}
add_filter('acf/pre_save_post', 'my_acf_pre_save_post', 10, 2);
acf/save_post
This action is run when ACF saves custom field data to a post. This function is not limited to the acf_form_head()
function and is used throughout the ACF plugin. For more information, please see the reference acf/save_post
function my_acf_save_post( $post_id ) {
// get new value
$value = get_field('my_field', $post_id);
// do something
}
add_action('acf/save_post', 'my_acf_save_post', 20);
acf/submit_form
This action is run after ACF has processed and saved the acf_form() $_POST data. Use this action to perform custom logic before the ‘return’ setting is used to redirect the browser. Added in version 5.5.10
function my_acf_submit_form( $form, $post_id ) {
// get new value
$value = get_field('my_field', $post_id);
// redirect
wp_redirect( 'http://www.website.com/' . $value );
exit;
}
add_action('acf/submit_form', 'my_acf_submit_form', 10, 2);