Creating a WordPress plugin to attach custom files to WooCommerce emails involves several steps. Below is a step-by-step guide and the necessary code to develop the plugin:
Step 1: Create Plugin Directory and Files
Create a new directory for your plugin within the wp-content/plugins directory. Name it something like wc-email-attachments.
Within this directory, create a main plugin file named wc-email-attachments.php.
Step 2: Plugin Header
Open wc-email-attachments.php and add the plugin header information.
<?php
/*
Plugin Name: WooCommerce Email Attachments
Plugin URI: https://example.com
Description: Easily attach custom files (pdf, doc, xls, zip, jpg, png, etc.) to any WooCommerce email.
Version: 1.0.0
Author: Your Name
Author URI: https://example.com
License: GPLv2 or later
Text Domain: wc-email-attachments
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
Step 3: Add Plugin Code
Add the necessary hooks and functions to handle file attachments to WooCommerce emails.
// Hook into WooCommerce email attachments filter
add_filter( 'woocommerce_email_attachments', 'add_custom_email_attachments', 10, 3 );
/**
* Add custom attachments to WooCommerce emails.
*
* @param array $attachments - Current list of attachments.
* @param string $email_id - Email ID.
* @param WC_Order $order - Order object.
* @return array - Modified list of attachments.
*/
function add_custom_email_attachments( $attachments, $email_id, $order ) {
// Define an array of emails to attach files to
$email_ids_to_attach = array(
'new_order',
'customer_processing_order',
'customer_completed_order',
// Add other WooCommerce email IDs as needed
);
// Retrieve the uploaded file path
$file_path = get_option('wc_email_attachments_custom_email_attachment');
// Check if the current email ID is one we want to attach files to
if ( in_array( $email_id, $email_ids_to_attach ) && file_exists($file_path) ) {
$attachments[] = $file_path;
}
return $attachments;
}
Step 4: Provide an Admin Interface (Optional)
If you want to provide an admin interface to upload and manage the files to be attached, you can create an options page in the WordPress admin area.
- Create an Admin Menu Page
// Hook into the admin menu
add_action( 'admin_menu', 'wc_email_attachments_add_file_upload_menu' );
/**
* Add an options page to the WordPress admin menu.
*/
function wc_email_attachments_add_file_upload_menu() {
add_menu_page(
'File Upload',
'File Upload',
'manage_options',
'wc-email-attachments-file-upload',
'wc_email_attachments_file_upload_plugin_page'
);
}
- Create the Upload images
// Create the Upload images
add_action('admin_init', 'upload_files');
/**
* Handle file uploads and store the file path in options.
*/
function upload_files() {
if (isset($_POST['upload_file'])) {
$uploaded_file = $_FILES['file_to_upload'];
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['path'] . '/' . basename($uploaded_file['name']);
// Move uploaded file to WordPress uploads directory
if (move_uploaded_file($uploaded_file['tmp_name'], $file_path)) {
$file_url = $upload_dir['url'] . '/' . basename($uploaded_file['name']);
// Save the file path in the options table
update_option('wc_email_attachments_custom_email_attachment', $file_path);
// Set a transient to indicate successful file upload
set_transient('wc_email_attachments_file_uploaded', true, 30); // 30 seconds expiry
// Redirect after upload to avoid resubmission issues
wp_redirect(admin_url('admin.php?page=wc-email-attachments-file-upload&upload_success=true'));
exit;
} else {
echo '<div class="error"><p>Sorry, there was an error uploading your file.</p></div>';
}
}
}
3. Handle File Uploads
/**
* Display file upload form and current uploaded file information.
*/
function wc_email_attachments_file_upload_plugin_page() {
// Check if file was uploaded successfully within the last 30 seconds
$file_uploaded = get_transient('wc_email_attachments_file_uploaded');
if ($file_uploaded) {
delete_transient('wc_email_attachments_file_uploaded');
$current_file_path = get_option('wc_email_attachments_custom_email_attachment');
$current_file_name = $current_file_path ? basename($current_file_path) : 'No file uploaded yet';
} else {
$current_file_name = 'No file uploaded yet';
}
// Display file upload form
?>
<div class="wrap">
<h2>File Upload Plugin</h2>
<?php if (isset($_GET['upload_success']) && $_GET['upload_success'] == 'true') : ?>
<div class="updated"><p>File uploaded successfully.</p></div>
<?php endif; ?>
<form method="post" enctype="multipart/form-data">
<table class="form-table">
<tbody>
<tr>
<th scope="row"><label for="file_to_upload">Upload File:</label></th>
<td><input type="file" name="file_to_upload" id="file_to_upload"></td>
</tr>
</tbody>
</table>
<p class="submit">
<input type="submit" name="upload_file" id="upload_file" class="button button-primary" value="Upload File">
</p>
</form>
<?php if ($current_file_name !== 'No file uploaded yet') : ?>
<div class="updated">
<p><strong>Current File:</strong> <?php echo esc_html($current_file_name); ?></p>
</div>
<?php endif; ?>
</div>
<?php
}
?>
Final Step: Activate Your Plugin
- Go to the WordPress admin dashboard.
- Navigate to Plugins -> Add New.
- Click “Upload Plugin” and choose your
wc-email-attachments.zip
file. - Install and activate the plugin.
This plugin will allow you to attach custom files to WooCommerce emails, either manually through the code or through an admin interface if you include the optional steps. Make sure to test the plugin thoroughly to ensure all features work as expected.