sync_noptin_subscribers_to_users( string|array $subscribers_to_sync = array() )

Synces existing subscribers to WordPress users.


Description Description

See also See also


Top ↑

Parameters Parameters

$subscribers_to_sync

(string|array) (Optional) The Noptin subscribers to sync to WordPress Users.

Default value: array()


Top ↑

Return Return

(void.)


Top ↑

Source Source

File: includes/subscriber.php

function sync_noptin_subscribers_to_users( $subscribers_to_sync = array() ) {

	// Arrays only please.
	$subscribers_to_sync = array_filter( noptin_parse_int_list( $subscribers_to_sync ) );

	foreach ( array_unique( $subscribers_to_sync ) as $subscriber_id ) {

		// Get the subscriber data...
		$subscriber = get_noptin_subscriber( $subscriber_id );

		// ... and abort if it is missing.
		if ( ! $subscriber->exists() ) {
			continue;
		}

		// If the subscriber is a WordPress user, continue.
		$user = get_user_by( 'email', $subscriber->email );
		if ( $user ) {
			update_noptin_subscriber_meta( $subscriber->id, 'wp_user_id', $user->ID );
			continue;
		}

		$username = trim( $subscriber->first_name . $subscriber->second_name );

		if ( empty( $username ) ) {
			$username = $subscriber->email;
		}

		// Prepare user values.
		$args = array(
			'user_login' => noptin_generate_user_name( $username ),
			'user_pass'  => wp_generate_password(),
			'user_email' => $subscriber->email,
			'role'       => 'subscriber',
		);

		$user_id = wp_insert_user( $args );
		if ( is_wp_error( $user_id ) ) {
			log_noptin_message(
				sprintf(
					__( 'WordPress returned the error: <strong>%s</strong> when syncing subscriber <em>%s</em>', 'newsletter-optin-box' ),
					$user_id->get_error_message(),
					sanitize_email( $subscriber->email )
				)
			);
			continue;
		}

		update_user_option( $user_id, 'default_password_nag', true, true ); // Set up the Password change nag.
		update_user_meta( $user_id, 'noptin_subscriber_id', $subscriber->id );
		update_noptin_subscriber_meta( $subscriber->id, 'wp_user_id', $user_id );
		wp_send_new_user_notifications( $user_id, 'user' );

	}

}


Top ↑

Changelog Changelog

Changelog
VersionDescription
1.2.3Introduced.

Top ↑

User Contributed Notes User Contributed Notes

30 day money back guarantee
Safe & Secure online payment
Custom CSS Slide Example
Custom Animation Example
Default Animation