• I have the following for WordPress hook but it doesn’t seem to be working at all. I am using the latest version of WordPress

    Here is the PHP CODE for functions.php

    //if you want none logged in users to access this function use this hook
    add_action('wp_ajax_nopriv_mail_before_submit', 'send_AJAX_mail_before_submit');
    
    function send_AJAX_mail_before_submit(){
        check_ajax_referer('my_email_ajax_nonce');
        if (isset($_POST['action']) && $_POST['action'] =="mail_before_submit"){
    
        //send email  wp_mail( $to, $subject, $message, $headers, $attachments ); ex:
            wp_mail('[email protected]','this is the email subject line','email message body');
            echo 'email sent';
            die();
        }
        echo 'error';
        die();
    }

    Here is the jquery Call

    jQuery(document).ready(function(){
    
            jQuery('#donationForm').submit(function() {
    
            // send email to client before moving onto worldpay payment form
            var data = {
                action: 'mail_before_submit',
                Whatever: 1234,
                _ajax_nonce: <?php echo wp_create_nonce( 'my_email_ajax_nonce' ); ?>
            };
            jQuery.post("<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php", data, function(response) {
                    alert('Got this from the server: ' + response);
            });
            //  send data to worldpay....
           // this.submit();
    
            });
    
        });

The topic ‘New hook not working’ is closed to new replies.