How do I collect an email address before sending the WordPress PDF?
You can ask visitors for an email address before they receive the generated PDF and save that address for your own follow-up workflow.
The plugin can already prompt for an email address and send the PDF by email. Saving the entered address is an extra customization that uses a PHP callback.
If you only want the plugin to email the generated PDF without storing the address, use How do I change what happens after clicking the Save as PDF button?.
Set the email options
In the plugin settings, open Expert Settings > Behavior.
Set
Button Click Action
to Send PDF via email.
Set
Email Recipient
to Prompt for an email address.
Set
PDF Created Callback
to save_pdfcrowd_email_address.
Add the callback function
Add this function to your theme's functions.php file or to a small
site-specific plugin:
function save_pdfcrowd_email_address($data) {
if ($data['error']) {
return false;
}
$email = isset($data['options']['email_to'])
? sanitize_email($data['options']['email_to'])
: '';
if (!$email || !is_email($email)) {
return false;
}
$entries = get_option('pdfcrowd_saved_email_addresses', array());
if (!is_array($entries)) {
$entries = array();
}
$entries[] = array(
'email' => $email,
'file_name' => sanitize_file_name($data['file_name']),
'created_at' => current_time('mysql'),
);
update_option('pdfcrowd_saved_email_addresses', $entries, false);
return false;
}
The entered address is available as email_to in the callback data. The
example stores each address in a WordPress option named
pdfcrowd_saved_email_addresses.
For production use, replace the storage part with your own database table, CRM integration, form plugin integration, or another storage location that matches your privacy and retention requirements.
Keep the normal email behavior
The callback must return false if the plugin should continue with its normal
email delivery. Return true only when your callback fully handles the result
and the plugin should skip its default email/download behavior.