Flexible ACF Frontend Form Shortcode for Custom Post Types

Home » Snippets » Flexible ACF Frontend Form Shortcode for Custom Post Types
0

Created with:

Visibility: 

public

Creator: Shanna Middleton

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
// Include the ACF form helper functions on the init hook
function wpturbo_acf_form_head() {
    if ( function_exists('acf_form_head') ) {
        acf_form_head();
    }
}
add_action('init', 'wpturbo_acf_form_head');

// Generic base ACF form shortcode function
function wpturbo_generic_acf_form_shortcode($atts) {
    // Check if the user is logged in
    if ( !is_user_logged_in() ) {
        return '<p>You must be logged in to create a profile.</p>';
    }

    // Parse the shortcode attributes
    $atts = shortcode_atts(array(
        'field_group' => '', // Default field group empty
        'post_type'   => 'profile', // Default post type 'profile'
        'new_post_status' => 'publish', // Default new post status 'publish'
    ), $atts, 'wpturbo_generic_form');

    // Prepare new post array
    $new_post = array(
        'post_type'     => $atts['post_type'],
        'post_status'   => $atts['new_post_status'],
        'field_groups'  => array($atts['field_group']),
    );

    // Prepare form options
    $options = array(
        'post_id'       => 'new_post',
        'new_post'      => $new_post,
        'post_title'    => true,
        'submit_value'  => 'Submit',
    );

    // Output buffering to capture the form
    ob_start();
    acf_form($options);
    $form = ob_get_clean();
    
    return $form;
}
add_shortcode('wpturbo_generic_form', 'wpturbo_generic_acf_form_shortcode');
				

Description: The WP Turbo ACF Frontend Form Shortcode enables WordPress site administrators and developers to easily integrate Advanced Custom Fields (ACF) forms into their website's frontend. This powerful functionality allows logged-in users to submit information through forms that are directly tied to any custom post type within the site.

Our shortcode, [wpturbo_generic_form], comes with a set of customizable attributes that give you control over the form's behavior and the resulting content that is created. By default, the shortcode is set up to handle a 'profile' post type, but this can be easily adjusted to suit any custom post type you have registered in your WordPress installation. This is done by changing the post_type attribute when using the shortcode, allowing for a seamless integration with your custom data structures.

To use the shortcode, simply add [wpturbo_generic_form] to any post or page content, and the ACF form will be displayed for logged-in users, allowing them to submit content that will be saved as a new post of the specified custom post type. The field_group attribute can be used to specify which ACF field group should be displayed in the form, ensuring that the right custom fields are available for user input.

In summary, this shortcode provides a flexible and user-friendly way to collect and manage user-submitted data through ACF forms, with the versatility to adapt to any custom post type, whether it's profiles, testimonials, products, or any other type of content your site requires.

Register an account to save your snippets or go Pro to get more features.