Absolutely! This was something I noticed the other day when I wanted to save values in the database and not the label! So if I’m understanding you right, you just want the shortcode in the mail template to use the value instead of the label. In that case, you’ll want to use the wpcf7_before_send_mail
hook like this:
/**
* Custom CF7 Staff Email
*
* @param WPCF7_ContactForm $form The current contact form being processed.
*
* @returns void
*/
function jglidewell_custom_staff_email($form)
{
// Check if this form is submitting the "staff" field
if (array_key_exists('staff', $_POST)) {
// Sanitize & validate staff value
$staff = trim(sanitize_email($_POST['staff']));
if (!empty($staff)) {
$mail = $form->prop('mail'); // Get the first mail property
if ($mail['active']) {
// If the first email is active, replace the shortcode with the value
$mail['additional_headers'] = str_replace('[staff]', esc_html($staff), $mail['additional_headers']);
}
$mail2 = $form->prop('mail_2'); // Get the second mail property
if ($mail2['active']) {
// If the second email is active, replace the shortcode with the value
$mail2['additional_headers'] = str_replace('[staff]', esc_html($staff), $mail2['additional_headers']);
}
// Update the form with the updated values
$form->set_properties(array(
'mail' => $mail,
'mail_2' => $mail2
));
}
}
}
add_action('wpcf7_before_send_mail', 'jglidewell_custom_staff_email');
See the Contact Form 7 documentation regarding accessing user input data for more info about the difference between using $_POST
and the posted data returned from $submission->get_posted_data()
.
Cheers!