AWeber WP Integration

By: adamcapriola on Oct 26th, 2011  |  syntax: PHP  |  size: 4.05 KB  |  hits: 90  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. //
  2. // Registration Form
  3. //
  4.  
  5. // Show extra fields on registration form
  6. add_action( 'register_form', 'show_extra_reg_fields', 1 );
  7.  
  8. function show_extra_reg_fields() { ?>
  9.         <p>
  10.         <label>First Name<br/>
  11.         <input type="text" name="first_name" id="first_name" class="input" value="<?php echo $_POST['first_name']; ?>" size="25" tabindex="30" />
  12.         </label>
  13.         </p>
  14.        
  15.         <p>
  16.         <label>Last Name<br/>
  17.         <input type="text" name="last_name" id="last_name" class="input" value="<?php echo $_POST['last_name']; ?>" size="25" tabindex="40" />
  18.         </label>
  19.         </p>
  20. <?php }
  21.  
  22. // Check if extra fields are filled out correctly
  23. add_filter( 'registration_errors', 'check_fields', 10, 3 );
  24.  
  25. function check_fields($errors, $login, $email) {
  26.                
  27.         if ( $_POST['first_name'] == '' && $_POST['last_name'] == '' ) {
  28.                 $errors->add('empty_realname', "<strong>ERROR</strong>: Please enter your first and last name");
  29.         }
  30.         elseif ( $_POST['first_name'] == '' ) {
  31.                 $errors->add('empty_realname', "<strong>ERROR</strong>: Please enter your first name");
  32.         }
  33.         elseif ($_POST['last_name'] == '') {
  34.                 $errors->add('empty_realname', "<strong>ERROR</strong>: Please enter your last name");
  35.         }
  36.        
  37.         return $errors;
  38. }
  39.  
  40. // Update user profile
  41. add_action( 'user_register', 'register_extra_fields' );
  42.  
  43. function register_extra_fields( $user_id ) {
  44.  
  45.         $userdata['ID'] = $user_id;
  46.         $userdata['first_name'] = ucwords(strtolower($_POST['first_name']));
  47.         $userdata['last_name'] = ucwords(strtolower($_POST['last_name']));
  48.        
  49.         wp_update_user($userdata);
  50. }
  51.  
  52. //
  53. // E-mail list MAGIC
  54. //
  55.  
  56. // Checkbox to join list
  57. add_action( 'register_form', 'email_opt_in' );
  58.  
  59. function email_opt_in() { ?>
  60.     <p>
  61.     <label style="font-size:13px;">
  62.     <input type="checkbox" name="email_opt_in" id="email_opt_in" class="input" checked="checked" value="1" <?php checked( $_POST['email_opt_in'], 1 ); ?> tabindex="99" style="width:12px;padding:0;margin:0 3px 0 0;font-size: 13px;" />
  63.         Join our famous e-mail list
  64.         </label>
  65.         </p>
  66.         <br/><?php
  67. }
  68.  
  69. // Join to e-mail list
  70. add_action( 'user_register', 'join_email_list' );
  71.  
  72. function join_email_list( $user_id ) {
  73.        
  74.         //
  75.         // 0. First Check if they Opted In, otherwise kill
  76.         //
  77.         if ( empty($_POST['email_opt_in']) ) return;
  78.        
  79.         //
  80.         // 1. Get WP Variables
  81.         //
  82.         $email = $_POST['user_email'];
  83.         $firstname = ucwords(strtolower($_POST['first_name']));
  84.         $lastname = ucwords(strtolower($_POST['last_name']));
  85.         $fullname = $firstname . ' ' . $lastname;
  86.        
  87.         // Encode them for URL
  88.         $email = urlencode( utf8_encode($email) );
  89.         $fullname = urlencode( utf8_encode($fullname) );
  90.        
  91.         //
  92.         // 2. Define Aweber Variables
  93.         //
  94.         $listname = 'somelist'; // YOUR LIST NAME
  95.         $adtracking = 'wpreg'; // AD TRACKING
  96.        
  97.         //
  98.         // 3. Build URL
  99.         //
  100.         $url = 'http://www.aweber.com/scripts/addlead.pl?listname=' . $listname . '&meta_adtracking=' . $adtracking . '&name=' . $fullname . '&email=' . $email . '&meta_message=1&redirect=http://www.aweber.com/form/thankyou_vo.html';
  101.        
  102.         //
  103.         // 4. Run the URL via CURL
  104.         //
  105.    
  106.     // build the individual requests as above, but do not execute them
  107.     $ch1 = curl_init( $url );
  108.    
  109.     // Options: http://se2.php.net/manual/en/function.curl-setopt.php
  110.     $options = array(CURLOPT_RETURNTRANSFER => true,
  111.                          CURLOPT_USERAGENT => 'Mozilla/5.0',
  112.                          CURLOPT_HEADER => false,
  113.                          CURLOPT_FOLLOWLOCATION => true,
  114.                          CURLOPT_TIMEOUT => 10,
  115.                          CURLOPT_FAILONERROR => true,
  116.                          CURLOPT_AUTOREFERER => true,
  117.                         );
  118.        
  119.         curl_setopt_array( $ch1, $options );
  120.            
  121.     // build the multi-curl handle, adding both $ch
  122.     $mh = curl_multi_init();
  123.     curl_multi_add_handle($mh, $ch1);
  124.    
  125.     // execute all queries simultaneously, and continue when all are complete
  126.     $running = null;
  127.     do {
  128.         curl_multi_exec($mh, $running);
  129.     } while ($running);
  130.    
  131.     // all of our requests are done, we can now access the results
  132.     //$html = curl_multi_getcontent($ch1);
  133.        
  134.         // close
  135.         curl_multi_remove_handle($mh, $ch1);
  136.         curl_multi_close($mh);
  137.        
  138. }