Hi,
I’ve created a questionnaire using acf_form set on user profiles with radio buttons fields.
this way – the results are visible in the user profile page in the admin area.
but now – I want to send the questions and the answers to my email automatically, so I have a backup of the user answers.
This is my form (that works perfectly)
$options = array(
'post_id' => 'user_'.$current_user->ID,
'field_groups' => array(232),
'form' => true,
'return' => add_query_arg( 'as', 'f', get_permalink() ),
'html_before_fields' => '',
'html_after_fields' => '',
'submit_value' => esc_html__( 'Continue', 'testtheme' )
);
acf_form( $options );
And this is my sending function:
(it’s the example from the documentation with small changes. It works great except for the form content)
add_action('acf/save_post', 'my_save_post');
function my_save_post( $post_id ) {
// bail early if editing in admin
if( is_admin() ) {
return;
}
// vars
$post = get_post( $post_id );
$name = 'Test site sender';
$email = '[email protected]';
// email data
$to = '[email protected]';
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n";
$subject = 'test';//$post->post_title;
$body = //HOW DO I GET THE FORM CONTENT HERE?;
// send email
wp_mail($to, $subject, $body, $headers );
}
Thanks ahead
Guy
Set the priority of your filter to 20 so that it runs after ACF has saved the values
add_action('acf/save_post', 'my_save_post', 20);
Then get the values and add them the same way you’d get them for a template
$body = '';
$body .= 'Field Label: '.get_field('field_name', $post_id)."\r\n";
$body .= 'field label 2:'.get_field('field_name_2, $post_id)."\r\n";
You could probably also use either get_fields() https://www.advancedcustomfields.com/resources/get_fields/ or get_field_objects() https://www.advancedcustomfields.com/resources/get_field_objects/ and use a loop to add them to the body. The main difference between a template and what you’re doing is that you need to add the values to the string rather then display them.
almost! 🙂
I got all the objects and been able to send them through email,
the only problem left is I get the chosen radio button value, and I need the chosen radio button’s name.
This is my relevant code now:
$body = '';
$fields = get_field_objects($post_id);
if( $fields )
{
foreach( $fields as $field_name => $field )
{
$body .= '<li>';
//the field label:
$body .= '<h3>' . $field['label'] . '</h3>';
//the chosen radio button value:
$body .= $field['value'];
$body .= '</li>';
}
}
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!
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.