• Resolved bjones1985

    (@bjones1985)


    The problem is my call back function which is mailchimp_submit isn’t working. Not sure why. This is what I have.

    add_action('wp_enqueue_scripts', 'mailchimp_scripts');
    
    function mailchimp_scripts() {
        wp_register_script( 'mailchimp', get_template_directory_uri().'/inc/plugins/mailchimp-single-sub/js/mailchimp-form-submit.js', array('jquery'), RARE_THEME_VERSION_NUMBER, true );
        wp_localize_script( 'mailchimp', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
    
        wp_enqueue_script( 'mailchimp' );
    }
    
    add_action('wp_ajax_mailchimp_submit', 'mailchimp_submit');
    add_action('wp_ajax_nopriv_mailchimp_submit', 'mailchimp_submit');
    
    function mailchimp_submit() {
    
        require_once __DIR__.'/inc/MCAPI.class.php';
        require_once __DIR__.'/inc/config.php';
    
        $api = new MCAPI($apikey);
    
        $subscriberemailID = $_POST["EMAIL"];
    
        $retval = $api->listSubscribe( $listId, $subscriberemailID, $merge_vars = null );
    
        echo json_encode($retrval);
    
        die();
    
    }
    
    // MailChimp form shortcode
    
    add_shortcode("mailchimp-single-sub-print", "mailchimp_single_sub");
    
    function mailchimp_single_sub() {
    
        echo '<div id="newsletter mc_embed_signup">
                <!-- Begin MailChimp Signup Form -->
                <div id="mailchimp_single_sub_widget_form">
                    <div id="status"></div>
                    <form class="newsletter-form" action="\" method="post">
                        <h2 class="signup-title">Subscribe to our mailing list</h2>
    
                        <div class="mc-field-group">
                            <input type="text" name="EMAIL" class="email required" value="" id="mce-EMAIL" placeholder="Email Address">
                            <input type="submit" name="subscribe" id="mc-embedded-subscribe" value="Subscribe" class="btn submit button">
                        </div>
                        <p class="signup-small">Get the latest stories and blogs posts emailed to you each day.</p>
                    </form>
                </div>
                <!-- End MailChimp Signup Form -->
                <div class="fix"></div>
            </div><!-- /#newsletter -->';
    
    }
    
    ?>

    The problem is the function mailchimp_submit is not working. The first file being required is the Mail Chimp API from MailChimp. The second file just includes the account API key and the list ID.

    Here is the Js file.

    jQuery(document).ready(function($) {
        $('#mc-embedded-subscribe').click(function() { 
    
            var hasError = false;
            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    
            var email = $("input.email").val();
            if (email == "") {
                alert('This is a required field');
                $("input.email").focus();
                hasError = true;
            }
    
            else if(!emailReg.test(email)) {
                alert('Please enter a valid email address');
                $("input.email").focus();
                hasError = true;
            }
    
            if(hasError == true) { return false; }
    
            $.ajax({
                type: 'POST',
                dataType : 'json',
                url: myAjax.ajaxurl,
                data: {action: 'mailchimp_submit'},
                success: function() {
                    $('.mc-field-group, .newsletter-form, .signup-small').hide();
                    $('#status').append('Thank you for subscribing.');
                }
             });
             return false;
        });
    });

    I have been reading the codex and tutorials on this specifically and can’t find a solution. If I could get any help would be appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter bjones1985

    (@bjones1985)

    UPDATE

    added this to the JS file data: {action: 'mailchimp_submit', 'EMAIL': email}, and it now sends email notification saying you are subscribed. But it doe not add the emails to the list in MailChimp.

    This is the function now

    add_action('wp_ajax_mailchimp_submit', 'mailchimp_submit');
    add_action('wp_ajax_nopriv_mailchimp_submit', 'mailchimp_submit');
    
    function mailchimp_submit() {
    
        require_once __DIR__.'/inc/MCAPI.class.php';
    
        $listId = '38e7d0812f';
    
        $api = new MCAPI('9100a95dd06efdbf58ec87bab613cee8-us5');
    
        $subscriberemailID = $_POST["EMAIL"];
    
        $retval = $api->listSubscribe( $listId, $subscriberemailID, $merge_vars = null );
    
    }

    Not sure why the emails are not added to the list in MailChimp but the confirmation emails are being sent out. Any suggestions????

    Moderator bcworkz

    (@bcworkz)

    The fact you are getting a confirmation email indicates you have correctly communicated with MailChimp. While failing to send the email data was certainly a glitch on the WP end, I now don’t see any other issues here. I know nothing of MCAPI though, so if there is something wrong there, I would not see it.

    Have you tried support at MailChimp? This no longer appears to be a WP issue. I hate passing the buck, but I’m out of my element with MailChimp.

    If that is your actual API key, you do not want it published here, add a ‘modlook’ tag to this thread so a moderator can remove or obfuscate it. Better yet, if possible, disable that key and get a new one. You can’t put a genie back in the bottle.

    Thread Starter bjones1985

    (@bjones1985)

    I appreciate the response. It isn’t the real API or list id. I think the main problem was there was a caching issue in my local dev environment. I left my computer for a couple of hours and when I came back it started working. It was magic lol.

    Thank you for responding though.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using Ajax in WordPress to submit a MailChimp form using MailChimp API’ is closed to new replies.