acf_form()


目录

Description

Displays a form to add or edit a post. There are many settings available to customize the form and these are set by adding to the $options array as explained below.

You may also register a form using the acf_register_form() function.

Parameters

<?php acf_form( $settings ); ?>

$settings

(String | Array) Array of settings or ‘id’ of a registered form.

Changelog

Examples

Editing current post

This example demonstrates how to display a basic form to edit the current post.

page-basic-form.php

<?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(); ?>

Editing a specific post

This example demonstrates how to display a form which edits the meta fields of a specific post.

page-specific-form.php

<?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(array(
        'post_id'       => 123,
        'post_title'    => false,
        'post_content'  => false,
        'submit_value'  => __('Update meta')
    )); ?>
    <?php endwhile; ?>
    </div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Create a specific post

This example demonstrates how to create a new post when submitting the form.

page-new-form.php

<?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(array(
        'post_id'       => 'new_post',
        'new_post'      => array(
            'post_type'     => 'event',
            'post_status'   => 'publish'
        ),
        'submit_value'  => 'Create new event'
    )); ?>
    <?php endwhile; ?>
    </div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Notes

Displaying within an AJAX modal

When displaying an ACF form within an AJAX modal (or any other dynamically appended method), please note that the page will require some extra PHP and JavaScript code.

acf_form_head()

It is important to note that whilst the acf_form() function will display a form, it will not process the form when submit. This task is handled by another function called acf_form_head(). To allow the form to save data, you will need to place the acf_form_head() function at the top of your page template before any HTML is rendered.

Security

Since version 5.6.5, ACF uses the wp_kses_post() function to sanitize content and strip out evil scripts. This sanitization can be disabled if needed by changing the form’s kses setting to false.


版本号 #1
由 壳壳虫 创建于 2026-08-29 15:44:03 UTC
由 壳壳虫 更新于 2026-08-29 15:44:03 UTC