Home / Admin / WP Simple Pay: Create WordPress User After Payment (Lite)
Duplicate Snippet

Embed Snippet on Your Site

WP Simple Pay: Create WordPress User After Payment (Lite)

Creates a new WordPress user when one-time payment is made with WP Simple Pay Lite.

Code Preview
php
<?php
/**
 * @link https://library.wpcode.com/snippet/32j68l5l/
 */
add_action(
	'simpay_payment_receipt_viewed', 
	/**
	 * @param array $payment_confirmation_data {
	 *   Contextual information about this payment confirmation.
	 *
	 *   @type \SimplePay\Vendor\Stripe\Customer $customer Stripe Customer
	 *   @type \SimplePay\Core\Abstracts\Form $form Payment form.
	 *   @type array<\SimplePay\Core\Vendor\Stripe\Subscription> $subscriptions Subscriptions associated with the Customer.
	 *   @type array<\SimplePay\Core\Vendor\Stripe\PaymentIntent> $paymentintents PaymentIntents associated with the Customer.
	 * }
	 */
	function( $payment_confirmation_data ) {
		if ( ! isset( $payment_confirmation_data['customer'] ) ) {
			return;
		}
		$customer      = $payment_confirmation_data['customer'];
		$email_address = $customer->email;
		// Don't create duplicate records if the page is reloaded.
		if ( false !== username_exists( $email_address ) ) {
			return;
		}
		// Creates a new user and notifies both the user and the site admin.
		//
		// @link https://developer.wordpress.org/reference/functions/wp_insert_user/
		// @link https://developer.wordpress.org/reference/functions/wp_new_user_notification/
		$user_id  = wp_insert_user( array(
			'user_email' => $email_address,
			'user_login' => $email_address,
			'nickname'   => $email_address,
			'role'       => 'subscriber',
			'user_pass'  => null,
		) );
		if ( false === is_wp_error( $user_id ) ) {
			wp_new_user_notification( $user_id, null, 'both' );
		}
	}
);

Comments

Add a Comment