Using acf_form to create a new post

Last updated Apr 7, 2015

Overview

This article will cover how to create a form on the front end of your website to add new content.

Getting started

If you have not already, please familiarize yourself with the acf form functions. Two functions are available to create a working form in a template file.

Name Description
acf_form_head() This function is placed at the top of a template file and will register the necessary assets (CSS/JS), process the saved data, and redirect the url. This function does not accepts any parameters
acf_form() This function is placed within a template file and will create the form’s HTML. This function accepts an array of settings to customize the form

Settings

The acf_form() parameter contains two settings called post_id and new_post. By using these settings correctly, a new post an be created using the form data.

The post_id setting  is used to edit an existing post, but when set to ‘new_post’, a new post will be created.

The new_post setting is used to specify an array of elements that make up a post. These elements can be found in the wp_insert_post()  documentation.

Examples

Basic

This form will create a new post.

page-new-post.php

<?php 

acf_form_head();

get_header();

?>
<div id="content">
    
    <?php
    
    acf_form(array(
        'post_id'       => 'new_post',
        'post_title'    => true,
        'post_content'  => true,
    ));
    
    ?>
    
</div>

<?php get_footer(); ?>

Post Type

This form will create a new published ‘event’ post.

page-new-post.php

<?php 

acf_form_head();

get_header();

?>
<div id="content">
    
    <?php
    
    acf_form(array(
        'post_id'       => 'new_post',
        'post_title'    => true,
        'post_content'  => true,
        'new_post'      => array(
            'post_type'     => 'event',
            'post_status'   => 'publish'
        )
    ));
    
    ?>
    
</div>

<?php get_footer(); ?>

Contact form

This example will make a basic contact form, send an email, and redirect the user to a thank-you page.

page-contact.php

<?php 

acf_form_head();

get_header();

?>
<div id="content">
    
    <?php
    
    acf_form(array(
        'post_id'       => 'new_post',
        'post_title'    => true,
        'post_content'  => true,
        'new_post'      => array(
            'post_type'     => 'contact_form',
            'post_status'   => 'publish'
        ),
        'return'        => home_url('contact-form-thank-you'),
        'submit_value'  => 'Send'
    ));
    
    ?>
    
</div>

<?php get_footer(); ?>

functions.php

add_action('acf/save_post', 'my_save_post');

function my_save_post( $post_id ) {
    
    // bail early if not a contact_form post
    if( get_post_type($post_id) !== 'contact_form' ) {
        
        return;
        
    }
    
    
    // bail early if editing in admin
    if( is_admin() ) {
        
        return;
        
    }
    
    
    // vars
    $post = get_post( $post_id );
    
    
    // get custom fields (field group exists for content_form)
    $name = get_field('name', $post_id);
    $email = get_field('email', $post_id);
    
    
    // email data
    $to = '[email protected]';
    $headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n";
    $subject = $post->post_title;
    $body = $post->post_content;
    
    
    // send email
    wp_mail($to, $subject, $body, $headers );
    
}

Related