Support » Plugin: MailPoet - emails and newsletters in WordPress » How get “Subscribe in registration form” list ids from MailPoet settings

  • Hello,

    I’m developing a plugin for registration forms and I’ve nearly managed everything to let the new user be subscribed on the lists by adding your checkbox on the form.
    The only thing I can’t get is the “Subscribe in registration form” lists, for now I can only get all the lists with this code:

    // Get available list to subscribe the subscriber
      	  $lists = $mailpoet_api->getLists();
    
      	  $list_ids = array();
    
      	  if (is_array($lists)) {
    	        foreach ($lists as $ids) {
    	        	$list_ids[] = $ids['id'];
    	        }
        	}

    But the user will be added to all the lists, I need it will be added only to the selected ones.

    Hope you can help me,
    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author MailPoet

    (@wysija)

    Hello @beeky2

    Thanks for your interest in developing a registration plugin that works with MailPoet. Unfortunately, it’s not very clear based on this snippet of code, what you’re trying to accomplish.

    I can see you’ve fetched the array of available lists using getLists(), and stored the IDs in a new array – $list_ids. Are you planning to use that array with subscribeToLists() function? How are you capturing the list IDs selected by your users? Perhaps you can provide more details about how you’re coding your plugin.

    Additionally, although we can’t provide you with exact codes for your custom project, we do have an extensive API documentation for developers like you who want to extend the MailPoet 3 plugin. Ref: https://github.com/mailpoet/mailpoet/tree/master/doc

    beeky2

    (@beeky2)

    Thanks for your reply.

    You got the point, I need to capture the list IDs selected by my users, but I don’t know how get that setting.

    This is the full function I’ve written for my plugin, as I told you at the moment is adding the subscribers to all the lists, but I need to add them only to the selected by the user (you plugin’s user).

    /**
     *
     * Mailpoet Integration
     *
     */
    add_action( 'user_register', 'swal_add_mailpoet_integration', 100 );
    
    function swal_add_mailpoet_integration($user_id) {
    
     	if (class_exists(\MailPoet\API\API::class)) {
    	  $mailpoet_api = \MailPoet\API\API::MP('v1');
    
    	  // Get available list to subscribe the subscriber
      	  $lists = $mailpoet_api->getLists();
    
      	  $list_ids = array();
    
      	  if (is_array($lists)) {
    	        foreach ($lists as $ids) {
    	        	$list_ids[] = $ids['id'];
    	        }
        	}	
    
    	  if (isset($_POST['mailpoet']['subscribe_on_register']) && (bool)$_POST['mailpoet']['subscribe_on_register'] === true) {
    
    	  	// Get user details
    	  	$user = get_user_by('id', $user_id);
    
    	  	if (!$user) {
    	  		return;
    	  	}
    
    	  	$email 			= $user->user_email;
    	  	$user_login 	= $user->user_login;
    	    $first_name 	= $user->first_name;
    	    $last_name 		= $user->last_name;
    
    	    // If first name is empty give it the username
    	    $first_name 	= $first_name ? $first_name : $user_login;
    
    		  // Check if subscriber exists. If subscriber doesn't exist an exception is thrown
    		  try {
    		    $get_subscriber = $mailpoet_api->getSubscriber($email);
    		  } catch (\Exception $e) {}
    
    		  try {
    		    if (!$get_subscriber) {
    		      // Subscriber doesn't exist let's create one
    		      $mailpoet_api->addSubscriber([
    									        'email' => $email,
    									        'first_name' => $first_name,
    									        'last_name' => $last_name,
    									      ], $list_ids);
    
    		    } else {
    		      // In case subscriber exists just add him to new lists
    		      $mailpoet_api->subscribeToLists($email, $list_ids);
    		    }
    		  } catch (\Exception $e) {
    		    $error_message = $e->getMessage(); 
    		  }
    		}
    
    	}
    }

    Do you have any update on this?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.