Front-End Submission Form With ACF
Adds an [acf_submit_form] shortcode that lets logged-in users submit a new post (saved as pending review) using your ACF field groups.
Opens WPIDE on
Code
// Field group keys shown in the form, and the post type created.
$wpide_acf_form_groups = array( 'group_wpide_post_extras' );
$wpide_acf_form_type = 'post';
add_action( 'template_redirect', function () {
// acf_form_head() must run before any output to process submissions.
if ( function_exists( 'acf_form_head' ) && is_user_logged_in() && is_singular() && has_shortcode( (string) get_post_field( 'post_content', get_queried_object_id() ), 'acf_submit_form' ) ) {
acf_form_head();
}
} );
add_shortcode( 'acf_submit_form', function () use ( $wpide_acf_form_groups, $wpide_acf_form_type ) {
if ( ! function_exists( 'acf_form' ) ) {
return '';
}
if ( ! is_user_logged_in() ) {
return '<p>Please <a href="' . esc_url( wp_login_url( get_permalink() ) ) . '">log in</a> to submit.</p>';
}
ob_start();
acf_form( array(
'post_id' => 'new_post',
'new_post' => array( 'post_type' => $wpide_acf_form_type, 'post_status' => 'pending' ),
'post_title' => true,
'field_groups' => $wpide_acf_form_groups,
'submit_value' => 'Submit for review',
'updated_message' => 'Thank you! Your submission is waiting for review.',
) );
return ob_get_clean();
} );
Notes
Set the field group key(s) and post type. Submissions wait for review; guests see a login link.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.