The easiest way would be to set the href URL to instead point to a PHP file that first sends the email you want, and then triggers a force download of the image file path that you specify.
Alternatively you could set an onclick
event to the link to execute Javascript to AJAX via /wp-admin/admin-ajax.php?action=
and then hook a function to the AJAX action in WordPress. There are many tutorials on available on how to add AJAX actions, but basically (where image_email
is the action
variable set in your AJAX call.):
add_action('wp_ajax_nopriv_image_email', 'custom_email_function'); // for logged out users
add_action('wp_ajax_image_email', 'custom_email_function'); // for logged in users
function custom_email_function() {
// send email code
}