acf_form()
Displays a form for adding or editing posts.
The form can also be registered using acf_register_form().
Using code in an AJAX modal
When displaying an ACF form in an AJAX modal (or by any other dynamically added method), note that this page requires additional PHP and JavaScript code.
-
PHP
Run the following function in the page body (inside the<body>tag) or through wp_footer. It creates hidden WYSIWYG fields required for JS templates and WordPress media popups.// Enqueue uploader scripts. acf_enqueue_uploader();
-
JS
Run the following JS after the AJAX request completes and the new HTML containing the ACF form is added to the DOM. This lets ACF initialize fields in the newly added HTML.// Trigger the append action and provide the newly appended jQuery element. acf.do_action( 'append', $('#popup-id') );
acf_form_head()
Although this function displays the form, it does not process it on submission. That task is performed by acf_form_head(). For the form to save data, place acf_form_head() at the top of the page template before any HTML is displayed.
Security
Since version 5.6.5, ACF uses wp_kses_post() to sanitize content and remove harmful scripts. When necessary, this sanitization can be disabled by setting the form’s kses setting to false.
No Hooks.
Returns
null. Nothing.
Usage
<?php acf_form( $settings ); ?>
- $settings(int/array)
An array of settings or the ID of a registered form. The array can contain the following keys:
$settings = [ 'id' => 'acf-form', 'post_id' => false, 'new_post' => false, 'field_groups' => false, 'fields' => false, 'post_title' => false, 'post_content' => false, 'form' => true, 'form_attributes' => [], 'return' => '', 'html_before_fields' => '', 'html_after_fields' => '', 'submit_value' => __("Update", 'acf'), 'updated_message' => __("Post updated", 'acf'), 'label_placement' => 'top', 'instruction_placement' => 'label', 'field_el' => 'div', 'uploader' => 'wp', 'honeypot' => true, 'html_updated_message' => '<div id="message" class="updated"><p>%s</p></div>', 'html_submit_button' => '<input type="submit" class="acf-button button button-primary button-large" value="%s" />', 'html_submit_spinner' => '<span class="acf-spinner"></span>', 'kses' => true ]- id(string) — unique form identifier. Default:
acf-form - post_id(Integer|String) — determines displayed and updated fields. Defaults to the current post ID;
new_postcreates a new post on saving. Default: false - new_post(array) — settings for a new post when
post_idisnew_post; see wp_insert_post(). Default: false - field_groups(array) — IDs/keys of field groups to override the displayed fields. Default: false
- fields(array) — field IDs/keys to override the displayed fields. Default: false
- post_title(Boolean) — whether to display a post-title field. Default: false
- post_content(Boolean) — whether to display the post-content field in the built-in WordPress editor. Default: false
- form(Boolean) — whether to create form elements; use when adding to an existing form. Default: true
- form_attributes(array) — an array of HTML attributes for the form element. Default: []
- return(string) — redirect URL after submission.
%post_url%becomes the post URL and%post_id%becomes the post ID. Default: '' - html_before_fields, html_after_fields(string) — additional HTML before or after fields. Default: ''
- submit_value(string) — submit-button label. Default:
__("Update", 'acf') - updated_message(string) — message displayed above the form after submission;
falsedisables it. Default:__("Post updated", 'acf') - label_placement(string) — label position:
toporleft. Default: 'top' - instruction_placement(string) — instruction position:
labelorfield. Default: 'label' - field_el(string) — field wrapper element:
div,tr,td,ul,ol, ordl. Default: 'div' - uploader(string) —
wpuploader orbasicinput for image and file fields. Default: 'wp' - honeypot(Boolean) — whether to add hidden fields that prevent bot form submissions. Default: true
- html_updated_message, html_submit_button, html_submit_spinner(string) — HTML used for the update message, submit button, and spinner.
- kses(Boolean) — whether to sanitize all $_POST data with wp_kses_post(). Default: true
- id(string) — unique form identifier. Default:
Examples
#1 Editing a simple post
This example demonstrates how to display a basic form for editing a simple post.
<?php acf_form_head(); ?> <?php get_header(); ?> <div id="primary" class="content-area"> <div id="content" class="site-content" role="main"> <?php while ( have_posts() ) : the_post(); ?> <?php acf_form(); ?> <?php endwhile; ?> </div><!-- #content --> </div><!-- #primary --> <?php get_sidebar(); ?> <?php get_footer(); ?>
#2 Edit title labels when enabled
If the form has 'post_title' => true, use acf/load_field/name=_post_title to change the title field’s label, placeholder, and instructions.
add_filter( 'acf/load_field/name=_post_title', 'air_post_title_acf_name' );
/**
* Changes the label, placeholder, and instructions for the title field in an ACF form.
*
* @param array $field ACF field data array.
* @return array The changed ACF field data array.
*/
function air_post_title_acf_name( $field ) {
$field['label'] = 'Enter the post title';
$field['placeholder'] = 'A post title is like air — you cannot do without it!';
$field['instructions'] = 'Be sure to provide a title for your post, otherwise it will remain nameless like a ghost.';
return $field;
}
#3 Editing specific post fields
This example demonstrates how to display a form that edits post meta-fields.
<?php acf_form_head(); ?>
<?php get_header(); ?>
<?php acf_form([
'post_id' => 123,
'post_title' => false,
'post_content' => false,
'submit_value' => __('Update meta')
]); ?>
<?php get_footer(); ?>
#4 Creating a custom post
This example shows how to create a new custom post using a form submission.
<?php acf_form_head(); ?> <?php get_header(); ?> <?php acf_form([ 'post_id' => 'new_post', 'new_post' => [ 'post_type' => 'event', 'post_status' => 'publish' ], 'submit_value' => 'Create new event' ]); ?> <?php get_footer(); ?>
acf_form() acf form code ACF 6.8.8
function acf_form( $args = array() ) {
acf()->form_front->render_form( $args );
}