Home › Forums › Front-end Issues › ACF form in front end based in ACF field › Reply To: ACF form in front end based in ACF field
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
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 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.