How to subscribe a new user to newsletters upon registration?

I'm guessing there would be a hook I could use, but what would I do to add a new user to the e-newsletter subscriptions?

Ideally, I have a new user registration form and on it I have a checkbox about subscriptions. I want to tie that to the database entry for adding the new user id to the default newsletter mailing list

  • Katya Tsihotska

    Hi madivad

    Hope you had a great day!

    It sounds possible and since this is dealing with more advanced coding topics, we'd need to ask our SLS team (code experts) for help. But before we do it, I need clarify some things.

    Ideally, I have a new user registration form and on it I have a checkbox about subscriptions. I want to tie that to the database entry for adding the new user id to the default newsletter mailing list

    Let me know, please, do you already have registration form customized this way or you just need to customize it? If you already have it, could you please let us know how did you customize it.

    Also, if your registration form is already customized, our support team might require some additional credentials to take a look at your site installation and settings. Please DO NOT post the credentials here as this is a public forum. You can send us your details through our contact form https://premium.wpmudev.org/contact/#i-have-a-different-question using this template:

    Subject: "Attn: Katya Tsihotska"
    -WordPress admin username
    -WordPress admin password
    -login URL
    -FTP credentials (host/username/password)
    -link back to this thread for reference
    -any other relevant URLs

    Please use this subject line to ensure that it gets assigned to me.

    Kind regards,
    Katya

  • madivad

    At the moment I am using a generic form for user registration, but it's part of a bigger package that is going to be utilised throughout the site as a whole. I'm integrating elements of WPMUDEV plugins (mostly membership, content protection and branding) with WooCommerce (product sales), Advanced Custom Fields and Formidable Forms (both pro versions). The last two being used to tie a lot of the little things together: contact form and registration for the front end; staff functionality, stock purchase orders and similar stuff for the backend.

    I haven't integrated the email subscriptions yet other than having a field in the user profile using ACF. I'm trying to work out who's action/filter hook I can use to best cross reference the user profile information with available newsletters. Is there an API/documentation between the membership2 pro and e-newsletters plugins?

  • Katya Tsihotska

    Hi madivad

    Thank you for detail description. I'll ping our SLS team to get their valuable feedback on this. One of our SLS members will reply back here soon. But please keep in mind that our SLS Team deals with more complicated issues, thus it may take a little longer for them to reply here.

    As for API Documentation, you can find Membership API Docs in your WordPress Dashboard > Membership 2 > Help > API Docs. Unfortunately, we haven't such documentation for e-Newsletter at the moment.

    Kind regards,
    Katya

  • Ivan

    Hi madivad !

    You can try to use MU plugin ( for example, create {WORDPRESS_FOLDER}/wp-content/mu-plugins/wpmu_subscribe_new_users.php via FTP ) with the following code:

    <?php
    
    function wpmu_subscribe_new_users( $user, $request, $obj ) {
    	global $email_newsletter;
    	if ( $email_newsletter && $request['NAME_OF_YOUR_CHECKBOX'] ) {
    		$groups_id = array( 1 );//here you should set id of member groups
    		$member_id = $email_newsletter->get_members_by_wp_user_id( get_current_user_id() );
    		if ( $member_id ) {
    			$email_newsletter->add_members_to_groups( $member_id, $groups_id, 1 );
    		}
    	}
    }
    
    add_action( 'ms_controller_frontend_register_user_complete', 'wpmu_subscribe_new_users', 10, 3 );

    Note: this code will work only for registration via Membership 2 Pro plugin.

    Best regards,
    Ivan.

  • Ivan

    Hi madivad !

    Yeah, you are right, it will be better to use user_register hook for all cases because plugins usually use wp_insert_user function for creating users where this hook is situated. But draw your attention to parameters for this hook - it has only $user_id, so final version of the MU plugin should look like

    <?php
    
    function wpmu_subscribe_new_users( $user_id ) {
    	global $email_newsletter;
    	if ( $email_newsletter && !empty( $_REQUEST['NAME_OF_YOUR_CHECKBOX'] ) {
    		$groups_id = array( 1 );//here you should set id of member groups
    		$member_id = $email_newsletter->get_members_by_wp_user_id( $user_id );
    		if ( $member_id ) {
    			$email_newsletter->add_members_to_groups( $member_id, $groups_id, 1 );
    		}
    	}
    }
    
    add_action( 'user_register', 'wpmu_subscribe_new_users' );

    Note: in this way it will subscribe a new user even if you will create him via Admin area and the checkbox is checked.

    Best regards,
    Ivan.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.