Support

Account

Home Forums General Issues Set Post Title on ACF Form Submit

Solved

Set Post Title on ACF Form Submit

  • I have an ACF form on the front end that creates an entry for a custom post type. I don’t want to show the post title field for the user to edit, but I want to set it to something like “News Item 123456” when the form is submitted, where 123456 uses time(). Below is my code for showing/submitting the form which does work.

    <?php
    // Show ACF Form On Front End Via ShortCode
    
    // acf_form_head(); added to header.php
    
    // 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');
    
    ?>
  • Add the below in your acf_form array.

    ‘post_title’ => ‘News Item ‘ . time(),

    When a new post is created it should be created with ‘News Item ‘ + unixt imestamp

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.