Programatically add new subscriber

Hello guys, I'm using a Contact Form 7 to get the name and email from my visitors and send me their messages. I'd like to automatically subscribe the visitors to my e-Newsletter list but there is no native integration between Contact Form 7 and e-Newsletter plugin. I'm wondering if I could extend Contact Form 7 to add the users to my subscribers list. What would be your advice for this scenario? Thanks!

  • Adam Czajczyk

    Hello Loremar Guimaraes,

    I hope you're well today and thank you for your question!

    There's no "native" integration of e-Newsletter with Contact Form 7 at this moment and I wouldn't expect it to be implemented anytime soon.

    Currently, the only solution would be to create a custom code that would add subscribers to the database. There's a DB add-on for Custom Form 7 here:

    https://wordpress.org/plugins/contact-form-7-to-database-extension/

    I think it could be "hacked"/converted to save data into E-Newsletter tables instead. E-Newsletter plugin uses multiple tables which are ("wp_" stands for your current install's db prefix defined in "wp-config.php" file):

    wp_enewsletter_groups
    wp_enewsletter_member_group
    wp_enewesletter_meta
    wp_enewsletter_newsletters
    wp_enewsletter_send
    wp_enewsletter_send_members
    wp_enewsletter_settings

    I think the most important ones here would be first 3 (for members subscriptions).

    If you feel like coding it yourself, don't hesitate to ask questions and I'll do my best to help though we do not provide custom development services. In case you'd want to get it developed for you you may want to post a question on our "Jobs & Pros" job board (please note: no WPMU DEV staff involved!) here:

    https://premium.wpmudev.org/wordpress-development/

    Best regards,
    Adam

  • Loremar Guimaraes

    Hello Adam,

    since you have the eNewsletter widget I decided to investigate the code behind it and extend my contact form using Javascript.

    I'm using something like this:

    function AddNewMember(member_email, member_name) {
    jQuery.ajax({
    method: "POST",
    url: "/wp-admin/admin-ajax.php",
    data: { action: "manage_subscriptions_ajax", newsletter_action: "new_subscribe", e_newsletter_email: member_email, e_newsletter_name: member_name, e_newsletter_auto_groups_id: "1", e_newsletter_add_groups_id: "1", e_newsletter_remove_groups_id: "1" }
    });
    }

    There is room for improvement because it's a hack but it's doing the job so far.

    Thank you!

  • Michelle

    For anyone getting here through search, I had to write a function that would accepts a User ID and Group Name and adds a user to that group in PHP using mySQL, and here it is if anyone wants it. You could easily make this add an array of users to a single group by adding a foreach loop around the second half.

    function addtoemailgroup ( $user_id, $group_name ) {
      if ( !empty($user_id) && !empty($group_name)  ) {
        global $wpdb;
    
        //Get the Group ID if it already exists
        $table_groups = $wpdb->prefix . 'enewsletter_groups';
        $sql = "
            SELECT group_id
            FROM {$table_groups}
            WHERE group_name LIKE '{$group_name}'
            LIMIT 1 ";
        $group_id = $wpdb->get_var($sql);
    
        //Create a new Group if the Group ID doesn't exist
        if ( empty($group_id) ) {
          $wpdb->insert(
            $table_groups,
            array(
              'group_name'  => $group_name,
              'public'      => 0
            )
          ) or die();
          //Save the new Group ID
          $group_id = $wpdb->insert_id;
        }
    
        //Find the Member ID of the user
        $table_members = $wpdb->prefix . 'enewsletter_members';
        $sql = "
          SELECT member_id
          FROM {$table_members}
          WHERE wp_user_id = {$user_id}
          LIMIT 1 ";
        $member_id = $wpdb->get_var($sql);
    
        // If the Member ID and Group ID both exist, add the member to the group
        if ( !empty($member_id) && !empty($group_id) ) {
          $table_member_group = $wpdb->prefix . 'enewsletter_member_group';
    
          //Check to see if the member is already in this group
          $sql = "
            SELECT count(member_id)
            FROM {$table_member_group}
            WHERE group_id = {$group_id}
            AND member_id = {$member_id}";
          $member_num = $wpdb->get_var($sql); // Result: 0 or 1.
    
          //If this user is not a member of this group, add them to it
          if ( $member_num == 0 ) {
            $wpdb->insert(
              $table_member_group,
              array(
                'member_id'  => $member_id,
                'group_id'   => $group_id
              )
            ) or die();
        }
      }
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.