Home › Forums › General Issues › ACF Front End Form Posting Twice
I have a fresh WP install and am working with an ACF front end form (I’ve used ACF before but this is my first time using a front end ACF form). Everything is working but it’s posting twice.
I’m adding the code via a custom plugin I created just for this, using a shortcode on the page. Below is the code I am using that posts the entry twice. Any ideas what could be causing this?
<?php
// Show ACF Form On Front End Via ShortCode
// Info from this video: https://www.youtube.com/watch?v=MSIc7rnZ2U0
// Code to show form
add_action('init', 'brandpage_form_head');
function brandpage_form_head() {
acf_form_head();
}
// Function for the shortcode
function bwp_acf_form_function($attr) {
// If ACF isn't running then exit
if (!function_exists('acf_form')) {
return;
}
// Buffer data instead of outputting it
ob_start();
// Unique form ID to avoid conflicts
$form_id = 'bwp-acf-form-' . uniqid();
// Debug: Log form ID and request data
error_log('Rendering ACF Form with ID: ' . $form_id);
error_log('Request data: ' . print_r($_REQUEST, true));
// Display ACF form
acf_form(array(
'id' => $form_id,
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'daily-message',
'post_status' => 'draft',
),
'field_groups' => array($attr['field_group_id']),
'post_title' => false, // False to auto-populate
'post_content' => false, // False to not use for content
'submit_value' => __("Add CSCC Daily Messenger Info", 'acf'),
'updated_message' => __("Daily Messenger Post Added", 'acf'),
'return' => '', // Redirect URL after saving (empty string means no redirect)
'html_after_fields'=> '<input type="hidden" name="my_form_nonce" value="' . wp_create_nonce('my_form_nonce_action') . '">', // Add nonce for security
));
// Empty the buffer data
return ob_get_clean();
}
// Add the shortcode
add_shortcode('bwp_acf_form', 'bwp_acf_form_function');
?>
Remove the init hook and try putting acf_form_head(); inside header.php
I had a similar problem before.
Thank you so much, this solved my issue! Four separate forums, multiple hours searching online and 2 AI bots before I could get the answer. Thank you!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.