Home › Forums › Front-end Issues › ACF form in front end based in ACF field
Excuse my English, I will try to be as clear as possible
I have tried to develop a routine to create form pages, something like a creator of forms systems.
Thus, the first need that I have identified is to have a standard form with the required fields to position a new page.
Thus, the first need that I have identified is to have a standard form with the required fields to position a new page
I created a page template and assigns a form with fields (page_customtype, page_idform, page_idpost)
Theoretically, these are the necessary information to position a form on a page on the front end
But when I run the code below, the form is not displayed
$idform = get_field('page_idform');
if ($idform!=null){
acf_form( array(
'post_id' => 'new_post_emp',
'field_groups' => array( $idform ),
'return' => add_query_arg( 'updated', 'true', "#" ),
));
}
I imagine that this takes place because the order of script execution, but we would have to get around this?
Oh dear, I’m a dumb!
My problem was that the reference ID was wrong, so would never work. But not to be so ugly, I share my finding.
The code to create a form based on another is:
Front End
<?php
$customtype = get_field('page_customtype');
$idform = get_field('page_idform');
$args = array(
'post_id' => 'newpost_'.$customtype ,
'field_groups' => array( $idform ),
'return' => add_query_arg( 'updated', 'true', "#" ),
);
acf_form( $args );
?>
And the part that makes me proud is how to treat the data to insert without having to have the reference (KeyField) of each field:
functions.php
function my_pre_save_post( $post_id ) {
$customtype = explode('_' , $post_id );
$post = array(
'post_status' => 'Publish',
'post_type' => $customtype[1],
);
$post_id = wp_insert_post( $post );
while (list($key, $value) = each($_POST['acf'])) {
$var = $var . "Key: $key; Value: $value<br />\n";
$namefield = get_field_object($key);
add_post_meta ($post_id, $namefield['name'] , $value, true);
}
}
add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
Thus a job that took hours (depending on the number of forms) only takes a few minutes.
I hope I can help in any way
The topic ‘ACF form in front end based in ACF field’ is closed to new replies.
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.