WordPress, Passing posted_data from Contact Form 7 via wpcf7_before_send_mail hook to FeedBurner

5.8k views Asked by At

I am developing a WordPress plugin that uses the Contact Form 7 wpcf7_before_send_mail action hook to grab the email that is entered in CF7 and, if the user select a checkbox, pass it the the email to MailChimp and FeedBurner.

The MailChimp portion is working because I am able to use an API to pass the subscription. However, from my research, it seems the only way to subscribe to FeedBurner is by using their form, which worked fine for a simpler version of this plugin I developed several months, however it seem that whenever I echo out anything (such as a hidden form) through the wpcf7_before_send_mail hook, it kills CF7. Here are the relavant portions of my code:

add_action( 'wpcf7_before_send_mail', 'before_send_mail' );
function before_send_mail($wpcf7) {
    if (is_array($wpcf7->posted_data["subscribe"])){
    $subscribe_news = in_array ('Newsletter',$wpcf7->posted_data["subscribe"]);
    $subscribe_blog = in_array ('Blog',$wpcf7->posted_data["subscribe"]);
    }
    $subscribe_email = $wpcf7->posted_data["your-email"];  
    $subscribe_email = $wpcf7->posted_data["your-email"];       
    if($subscribe_news){ ktmcf7_submit_mailchimp($subscribe_email); }
    if($subscribe_blog){ ktmcf7_submit_feedburner($subscribe_email); }    
    }
function ktmcf7_submit_feedburner($subscribe_email){
    $options = get_option( 'ktm_singlesub_options' );
    ?>
    <script>
        alert('feedburner');
        window.open('http://feedburner.google.com/fb/a/mailverify?uri=<?php echo $options['feedburner_id'] ?>', 'popup5', 'scrollbars=yes,width=550,height=520');
    </script>
    <form name="form2" action="http://feedburner.google.com/fb/a/mailverify" method="post" target="popup5" >
        <input type="hidden" name="email" value="<?php echo $subscribe_email ?>" />
        <input type="hidden" value="<?php echo $options['feedburner_id'] ?>" name="uri"/>
        <input type="hidden" name="loc" value="en_US"/>
        <!-- input type="submit" value="Subscribe2" style="visibility:hidden;height:5px;" / -->
    </form>
    <script>
        document.form2.submit();
    </script>

Again, I'ved tried several different ways to output to the browser from this hook (echo, var_dump, etc, and each time the arrouw just spun infinitely. How can i work around this? Is there another way to submit a subscription to Feedburner other than using a form. There web site says the API is no longer available, but is there a secret back door?

Thanks.

1

There are 1 answers

0
Bill On

In theory, you don't want to output a form, you want to submit the form variables using an HTTP post to FeedBurners end point using the wp_ function for issuing HTTP POSTs.

The following code implements a WordPress Plugin that will issue an HTTP POST from Contact Form 7's wpcf7_before_send_mail event handler:

function wpcf7_do_something (&$WPCF7_ContactForm) {
    $url = 'http://your-end-point';
    $email = $WPCF7_ContactForm->posted_data['email'];    
    $post_data = array(
         'email' => urlencode($email),
         'feedburner_id' => urlencode($feedburner_id));

    $result = wp_remote_post( $url, array( 'body' => $post_data ) );
}

add_action("wpcf7_before_send_mail", "wpcf7_do_something");

The above code works as advertised, but keep in mind you might need to deal with CSRF tokens as well. https://en.wikipedia.org/wiki/Cross-site_request_forgery