Support

Account

Home Forums General Issues Using acf_form – add post title…

Solved

Using acf_form – add post title…

  • Hi Elliot,

    I am using acf_form() to create a front-end form for users, and this is all great, but I’m curious to know only one thing:

    1. How can you add a post title to the form? I know in the functions.php form includes the line, from your documentation, 'post_title' => 'A title, maybe a $_POST variable', which can be edited, but I want the user to enter their own title. I’ve had a look around, and tried adding this:

    'html_before_fields' => '<h2>Add New Post</h2><input type="text" name="post_title" size="30" id="title" placeholder="Enter title here" autocomplete="off">',

    to the acf_form() arguments, but it has no effect. When I add a title and click Submit, it adds the post no problem but the title is missing…

    Thanks in advance,
    R

  • You need to use ACF’s acf/save_post action hooks for this. I have seen this code somewhere here in the forum, but can’t find the link to that, so I’m pasting it below:

    function ps_acf_save_post( $post_id ) {
        // Don't do this on the ACF post type
        if ( get_post_type( $post_id ) == 'acf' ) return;
    
        // Get the Fields
        $fields = get_field_objects( $post_id );
    
        // Prevent Infinite Looping...
        remove_action( 'acf/save_post', 'my_acf_save_post' );
    
        // Grab Post Data from the Form
        $post = array(
            'ID'           => $post_id,
            'post_type'    => 'post',
            'post_title'   => $fields['new_title']['value'],
            'post_content' => $fields['new_description']['value'],
            'post_status'  => 'publish'
        );
    
        // Update the Post
        wp_update_post( $post );
    
        // Continue save action
        add_action( 'acf/save_post', 'my_save_post' );
    
        // Set the Return URL in Case of 'new' Post
        $_POST['return'] = add_query_arg( 'updated', 'true', get_permalink( $post_id ) );
    }
    add_action( 'acf/save_post', 'ps_acf_save_post', 10, 1 );

    new_title is a my custom field to be replicated for the Title and new_description for the content.

  • Thanks for the reply, @junixvillacorta – so I have added this in (I did have this… but used your code now so we’re on the same page) but it still doesn’t seem to be saving the title…

    <?php $options = array(
    	'post_id' => 'new',
    	'html_before_fields' => '<input type="text" name="post_title" size="30" id="title" placeholder="Enter title here" autocomplete="off">',
    	'field_groups' => array(835),
    	'submit_value' => 'Submit your listing'
    ); ?>
    
    <div class="container_12">
    	<div class="prefix_3 grid_9">
    		<div class="submit-form-container">
    			<h2>Submit your listing</h2>
    			<?php acf_form($options); ?>
    		</div>
    	</div>
    </div>
    <div class="clear"></div>
    // Create the frontend form
    function ps_acf_save_post( $post_id ) {
        // Don't do this on the ACF post type
        if ( get_post_type( $post_id ) == 'acf' ) return;
    
        // Get the Fields
        $fields = get_field_objects( $post_id );
    
        // Prevent Infinite Looping...
        remove_action( 'acf/save_post', 'my_acf_save_post' );
    
        // Grab Post Data from the Form
        $post = array(
            'ID'           => $post_id,
            'post_type'    => 'events',
            'post_title'   => $fields['new_title']['value'],
            'post_status'  => 'draft'
        );
    
        // Continue save action
        add_action( 'acf/save_post', 'my_save_post' );
    
        // Set the Return URL in Case of 'new' Post
        $_POST['return'] = add_query_arg( 'updated', 'true', get_permalink( $post_id ) );
    }
    add_action( 'acf/save_post', 'ps_acf_save_post', 10, 1 );
  • you need to remove this code:
    'html_before_fields' => '<input type="text" name="post_title" size="30" id="title" placeholder="Enter title here" autocomplete="off">',

    you need to create a text custom field that you will assign to be your title. The code below assigns the custom field to be the post title:

    $post = array(
            'ID'           => $post_id,
            'post_type'    => 'events',
            'post_title'   => $fields['new_title']['value'],
            'post_status'  => 'draft'
        );

    please also include this code inside the function before the // Continue save action. I completely forgot about it:

    // Update the Post
        wp_update_post( $post );

    edited my post above as well and added this code.

  • Thanks junixvillacorta, okay this makes sense. I wonder why I can’t use the standard title field for this? Why do I have to create duplicate field for title? This means on my form, on the CMS end (not front-end) it’ll have to title fields.

  • @junixvillacorta when I use the code you provided and now in all my custom fields the content that I put in there for that first post is stuck there all the time. When I refresh the page or try a different browser the forms are still populated with the original content that I put in.

  • @greenhoe Is right – this also happens to me. And it no longer actually creates the post either. And actually the only way to clear those form fields which stayed permanent was to delete the rows from the wp_options table. Weird, but definitely not the solution.

    All I need is the ability to add the post title.

  • Worked this out. @greenhoe hope this helps.

    // Create the frontend form
    function my_pre_save_post( $post_id ) {
    	if ( $post_id != 'new' ) {
    		return $post_id;
        }
        $post = array(
            'post_status' => 'draft',
            'post_title' => $_POST['fields']['field_5344021b6851b'],
            'post_type' => 'events'
        );  
        $post_id = wp_insert_post($post); 
        $_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );    
        return $post_id;
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );

    Now ACF allows a $_POST variable to be used for the title but does not really state how to use it. We create a title field in ACF and then used the field name that was automatically generated from ACF. So, as above:

    'post_title' => $_POST['fields']['field_5344021b6851b'],

    field_5344021b6851b is the name of my title field I created.

    Hope this helps. Worked for me. Let me know if you have any problems.
    -R

  • This is possible in ACF 5 using the new “post_title” parameter in the acf_form function.

  • rdck, I’ve done what you said:

    ‘post_title’ => $_POST[‘fields’][‘field_5344021b6851b’],

    Mine was:
    $_POST[‘fields’][‘nome_do_evento’],

    And it doesn’t work.

    Can you figure it out on my functions.php?

    
    // Create the frontend form
    function my_pre_save_post( $post_id ) {
    	if ( $post_id != 'new' ) {
    		return $post_id;
        }
        $post = array(
            'post_status' => 'draft',
            'post_title' => 'teste', $post_title, $_POST['nome_do_evento']['value'],
            'post_type' => 'post'
        );  
        $post_id = wp_insert_post($post); 
        $_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );    
        return $post_id;
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    

    And the page.php :

    			
     <?php $acf = array(
    	'post_id' => 'new',
    	'field_groups' => array(15),
    	'submit_value' => 'ENVIAR'
    ); ?>
    			
    			
    <?php acf_form($acf); ?>
    
  • Nevermind!

    I got it! The field number: [‘field_5344021b6851b’],
    Appeared after I inspected the element.

  • I have the same problem and I use ACF PRO 5 I only have:
    <input type=”text” id=”acf-_post_title” class=”” name=”acf[_post_title]” value=”” placeholder=””>

    when I inspect the field

    How can I save the post title ? same question for post content

    Thank you very much 🙂

  • I modified a a plugin which I forked and corrected.

    You can find it here

    https://bitbucket.org/kolhoffmmm/acf-edit-title-content

  • Inspect it at the browser, the html.

  • I am using the 'form' => false, option to provide the ACF fields within my own form HTML.

    I just added my own plain html field with the id acf-_post_title and name acf[_post_title] and everything worked as expected.

    <input type="hidden" id="acf-_post_title" name="acf[_post_title]" value="Anonymous Response">

    I provided a default title that worked for me, and then used a bit of jQuery to change the value based on user input to other fields. My example fills in the first and last name fields if they are not empty and if an anonymous checkbox is not checked.

    $('#acf-field_590bacbe30591, #acf-field_590bacd630592, #acf-field_590bad1230595').on('change', function() {
    	var fname = $('#acf-field_590bacbe30591').val();
    	var lname = $('#acf-field_590bacd630592').val();
    	var anon = $('#acf-field_590bad1230595').attr('checked');
    	if ( anon == 'checked' || ( fname == '' && lname == '' ) ) {
    		$('#acf-_post_title').val("Anonymous Response");
    	}
    	else {
    		$('#acf-_post_title').val("Response from " + fname + " " + lname);
    	}
    });
Viewing 15 posts - 1 through 15 (of 15 total)

The topic ‘Using acf_form – add post title…’ is closed to new replies.