Home › Forums › General Issues › Publishing to custom fields from front end?
I have a simple demo form that will publish to my custom post type from the front end (it’s on a local offline server). However, it only works with the default wordpress fields (title and content), at the moment.
Does anyone know how to add a custom field from ACF to this form? I’ve searched Google and having trouble finding anything.
Thanks in advance,
Michael Newsome
Can you give more details about what you mean by “publish to my custom post type from the front end”? Are you accepting input from the front end to create custom posts?
Can you post the code that is generating your form?
What type of fields do you want to add to the form and what fields are already on the form?
It’s a custom post type called Events. Right now, it’s just a textbox called Test Field. For now, I need to be able to put textbox and textarea on the front end (but would like to maybe put a repeater there, if possible, but I’m not sure that it is).
This is above the header:
<?php
if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) {
if ( trim( $_POST['postTitle'] ) === '' ) {
$postTitleError = 'Please enter a title.';
$hasError = true;
}
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'events',
'post_status' => 'publish'
);
wp_insert_post( $post_information );
}
?>
And this is the actual form:
<form action="" id="primaryPostForm" method="POST">
<fieldset>
<label for="postTitle"><?php _e('Post Title:', 'framework') ?></label>
<input type="text" name="postTitle" id="postTitle" class="required" value="<?php if ( isset( $_POST['postTitle'] ) ) echo $_POST['postTitle']; ?>" />
</fieldset>
<fieldset>
<label for="postContent"><?php _e('Post Content:', 'framework') ?></label>
<textarea name="postContent" id="postContent" rows="8" cols="30" class="required"><?php if ( isset( $_POST['postContent'] ) ) { if ( function_exists( 'stripslashes' ) ) { echo stripslashes( $_POST['postContent'] ); } else { echo $_POST['postContent']; } } ?></textarea>
</fieldset>
<fieldset>
<input type="hidden" name="submitted" id="submitted" value="true" />
<button type="submit"><?php _e('Add Post', 'framework') ?></button>
<?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
</fieldset>
</form>
<?php if ( $postTitleError != '' ) { ?>
<span class="error"><?php echo $postTitleError; ?></span>
<div class="clearfix"></div>
<?php } ?>
When I post to the Events post type from the front end, I just need to be able to put data in these custom fields, also.
Ahh, I see what you’re trying to do. I’ve never done it, and can’t answer your question. But I did find something that might steer you in the right direction.
This looks like a good start:
http://www.advancedcustomfields.com/resources/using-acf_form-to-create-a-new-post/
Then this answers the first question I had looking at the example:
http://support.advancedcustomfields.com/forums/topic/front-end-creat-post-2/
As far as the repeater goes, it’s pretty awesome and it took me a bit to wrap my head around at first. It’s basically a group of fields that a user can keep adding new instances of.
I hope this helps and good luck.
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!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.