How to save email address when creating PDF?

If you want to collect email addresses of visitors to your website who clicked the Save as PDF plugin button, follow these steps:

1. Go to the plugin settings and on the Behavior tab:

  • Set "Button Click Action" to "Send PDF via email"
  • Set "Email Recipient" to "Prompt for an email address"
  • Set "PDF Created Callback" to save_email

2. Define the save_email callback function, that will be called after PDF is created. You can add it to the end of the function.php file of your theme. The code can look like this:

function save_email($data) {
    if(!$data['error']) {
        $uploads = wp_upload_dir('2023/01');
        if(!$uploads['error']) {
            $file_path = $uploads['path'] . '/addresses.txt';
            $result = file_put_contents(
                $file_path, $_POST['recipient'] . "\n", FILE_APPEND);
            if($result === false) {
                error_log('Failed to write to the addresses file.');
            }
        } else {
            error_log('Upload dir error: ' . $uploads['error']);
        }
    }
}

The email addresses will be stored in file 2023/01/addresses.txt in the WordPress uploads folder.