Ask your WordPress questions! Pay money and get answers fast! (more info)

My front end form isn't inserting a value for an ACF field

  • SOLVED

Hi,

I'm essentially building a testimonials feature for a customer. I have a front end form which is called as a function and then a function to handle the insertion of the post.

Within the testimonials page, I have a star rating field which is created with Advanced Custom Fields. For some reason, all of the content submitted via the form is being added to a post, but without the ACF field.

I can't work it out, would love some help! Code below:

function ty_front_end_form() {
?>
<form id="custom-post-type" name="custom-post-type" method="post" action="">

<p><label for="title">Your Name</label><br />

<input type="text" id="title" value="" tabindex="1" size="20" name="title" />

</p>
<p><label for="stars">Your Rating</label><br />
<input type="radio" name="stars" id="stars" value="1"> 1 star
<input type="radio" name="stars" id="stars" value="2"> 2 stars
<input type="radio" name="stars" id="stars" value="3"> 3 stars
<input type="radio" name="stars" id="stars" value="4"> 4 stars
<input type="radio" name="stars" id="stars" value="5"> 5 stars

<p><label for="description">Your Testimonial</label><br />

<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>

</p>

<p align="right"><input type="submit" value="Submit your testimonial" tabindex="6" id="submit" name="submit" /></p>

<input type="hidden" name="post-type" id="post-type" value="custom_posts" />

<input type="hidden" name="action" value="custom_posts" />

<?php wp_nonce_field( 'name_of_my_action','name_of_nonce_field' ); ?>

</form>
<?php
if($_POST){
ty_save_post_data();
}

}


And the form handler:

function ty_save_post_data() {

if ( empty($_POST) || !wp_verify_nonce($_POST['name_of_nonce_field'],'name_of_my_action') )
{
print 'Sorry, your nonce did not verify.';
exit;

}else{

// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter your name';
exit;
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter your testimonial';
exit;
}
if (isset ($_POST['stars'])) {
$stars = $_POST['stars'];
} else {
echo 'Please enter a rating';
exit;
}


// Add the content of the form to $post as an array
$post = array(
'post_title' => wp_strip_all_tags( $title ),
'customer' => wp_strip_all_tags( $title ),
'post_content' => wp_strip_all_tags ( $description ),
'post_status' => 'pending', // Choose: publish, preview, future, etc.
'post_type' => 'testimonial',
'test' => $title,
'star-rating' => $stars // Use a custom post type if you want to
);
wp_insert_post($post); // http://codex.wordpress.org/Function_Reference/wp_insert_post

$location = home_url(); // redirect location, should be login page

echo "<meta http-equiv='refresh' content='0;url=$location' />"; exit;
} // end IF

Answers (2)

2015-08-28

Andrea P answers:

Acf fields (and meta fields in general) cannot be added with wp_insert_post.

fortunately that function creates the post AND also returns the id of the newly created post.
so after you run it, you can use an ACF function to update the value of the field


// Add the content of the form to $post as an array

$post = array(
'post_title' => wp_strip_all_tags( $title ),
'customer' => wp_strip_all_tags( $title ),
'post_content' => wp_strip_all_tags ( $description ),
'post_status' => 'pending', // Choose: publish, preview, future, etc.
'post_type' => 'testimonial',
'test' => $title,
);

$post_id = wp_insert_post($post); // http://codex.wordpress.org/Function_Reference/wp_insert_post

if ( !is_wp_error($post_id) ){
// update the field
update_field('star-rating', $stars, $post_id);

}


here are more info about the update_field() function:
http://www.advancedcustomfields.com/resources/update_field/


sebdean comments:

Thank you, that looks to have fixed it!

2015-08-28

S├ębastien | French WordpressDesigner answers:

star-rating (and "test" and "customer") is not a key you can set for a post

https://codex.wordpress.org/Function_Reference/wp_insert_post


The contents of the post array can depend on how much (or little) you want to trust the defaults. Here is a list with a short description of all the keys you can set for a post:

$post = array(
'ID' => [ <post id> ] // Are you updating an existing post?
'post_content' => [ <string> ] // The full text of the post.
'post_name' => [ <string> ] // The name (slug) for your post
'post_title' => [ <string> ] // The title of your post.
'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ] // Default 'draft'.
'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] // Default 'post'.
'post_author' => [ <user ID> ] // The user ID number of the author. Default is the current user ID.
'ping_status' => [ 'closed' | 'open' ] // Pingbacks or trackbacks allowed. Default is the option 'default_ping_status'.
'post_parent' => [ <post ID> ] // Sets the parent of the new post, if any. Default 0.
'menu_order' => [ <order> ] // If new post is a page, sets the order in which it should appear in supported menus. Default 0.
'to_ping' => // Space or carriage return-separated list of URLs to ping. Default empty string.
'pinged' => // Space or carriage return-separated list of URLs that have been pinged. Default empty string.
'post_password' => [ <string> ] // Password for post, if any. Default empty string.
'guid' => // Skip this and let Wordpress handle it, usually.
'post_content_filtered' => // Skip this and let Wordpress handle it, usually.
'post_excerpt' => [ <string> ] // For all your post excerpt needs.
'post_date' => [ Y-m-d H:i:s ] // The time post was made.
'post_date_gmt' => [ Y-m-d H:i:s ] // The time post was made, in GMT.
'comment_status' => [ 'closed' | 'open' ] // Default is the option 'default_comment_status', or 'closed'.
'post_category' => [ array(<category id>, ...) ] // Default empty.
'tags_input' => [ '<tag>, <tag>, ...' | array ] // Default empty.
'tax_input' => [ array( <taxonomy> => <array | string>, <taxonomy_other> => <array | string> ) ] // For custom taxonomies. Default empty.
'page_template' => [ <string> ] // Requires name of template file, eg template.php. Default empty.
);


you can eventually display the rating in the content like that for example :




/ Add the content of the form to $post as an array

$post = array(

'post_title' => wp_strip_all_tags( $title ),

'post_content' => wp_strip_all_tags ( $description." rating=".$stars ),

'post_status' => 'pending', // Choose: publish, preview, future, etc.

'post_type' => 'testimonial'

);

wp_insert_post($post); // http://codex.wordpress.org/Function_Reference/wp_insert_post



sebdean comments:

Hi,

OK, so is there a way that I can insert into star-rating?


S├ębastien | French WordpressDesigner comments:

I have edit my first response to explain how insert the star-rating in the content for example


sebdean comments:

Thanks for the reply. I'd like to be able to insert content into the star-rating field though, surely there's a way to do this?