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

Embed Snippet on Your Site

WP Simple Pay: Create WordPress User After Payment

Creates a new WordPress user when a subscription is started or a one-time payment is made.

Code Preview
php
<?php
/**
 * @link https://library.wpcode.com/snippet/3234p8or/
 * 
 * @param \Stripe\Event $event Stripe Event.
 * @param \Stripe\Subscription|\Stripe\PaymentIntent $object Stripe Subscription or PaymentIntent
 */
function simpay_custom_create_user( $event, $object ) {
	$email_address = $object->customer->email;
	// Don't create duplicate records.
	if ( false !== username_exists( $email_address ) ) {
		throw new \Exception( 'Email address already exists.' );
	}
	// 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,
		'user_pass'  => null,
	) );
	if ( is_wp_error( $user_id ) ) {
		throw new \Exception( $user_id->get_error_message() );
	}
	
	$user = get_user_by( 'id', $user_id );
	$user->add_role( 'subscriber' );
	wp_new_user_notification( $user_id, null, 'both' );
}
add_action( 'simpay_webhook_subscription_created', 'simpay_custom_create_user', 10, 2 );
add_action( 'simpay_webhook_payment_intent_succeeded', 'simpay_custom_create_user', 10, 2 );

Comments

Add a Comment

  1. Do you have a version of this snippet that will assign a different role based on the price the user paid? For example if there are 3 prices, each associated with a membership level.

    1. Hello James,

      You can access the amount that was paid with the following:

      “`
      if ( ‘invoice’ === $object->type ) {
      $amount = $object->amount_paid;
      } else {
      $amount = $object->amount;
      }
      “`

      You can then perform different actions based on the amount paid. I hope that helps!

      1. This seems to be working, sort of, however, it’s ignoring my conditions and just assigned the first $user_id it’s encountering, if though it’s contained with a conditional statement. Do you seen any obvious issue here?

        if ($amount = “35.00”) {
        $user_id = wp_insert_user( array(
        ‘user_email’ => $email_address,
        ‘user_login’ => $email_address,
        ‘nickname’ => $email_address,
        ‘role’ => ‘subscriber’,
        ‘user_pass’ => null,
        ) );
        } elseif ($amount = “100.00”) {
        $user_id = wp_insert_user( array(
        ‘user_email’ => $email_address,
        ‘user_login’ => $email_address,
        ‘nickname’ => $email_address,
        ‘role’ => ‘first_tracks’,
        ‘user_pass’ => null,
        ) );
        } elseif ($amount = “500.00”) {
        $user_id = wp_insert_user( array(
        ‘user_email’ => $email_address,
        ‘user_login’ => $email_address,
        ‘nickname’ => $email_address,
        ‘role’ => ‘hut_hero’,
        ‘user_pass’ => null,
        ) );
        }

        1. Sorry, I pasted incorrect code since I am checking to see if the variable equals a value, but this still doesn’t behave as expected:

          if ($amount == ’35’) {
          $user_id = wp_insert_user( array(
          ‘user_email’ => $email_address,
          ‘user_login’ => $email_address,
          ‘nickname’ => $email_address,
          ‘role’ => ‘subscriber’,
          ‘user_pass’ => null,
          ) );
          } elseif ($amount == ‘100’) {
          $user_id = wp_insert_user( array(
          ‘user_email’ => $email_address,
          ‘user_login’ => $email_address,
          ‘nickname’ => $email_address,
          ‘role’ => ‘first_tracks’,
          ‘user_pass’ => null,
          ) );
          } elseif ($amount == ‘500’) {
          $user_id = wp_insert_user( array(
          ‘user_email’ => $email_address,
          ‘user_login’ => $email_address,
          ‘nickname’ => $email_address,
          ‘role’ => ‘hut_hero’,
          ‘user_pass’ => null,
          ) );
          }

          1. After some go rounds, I reverted back to your original code with one change: I changed
            ‘role’ => ‘subscriber’,
            to
            ‘role’ => ‘foundation’,
            Then, after my test, it created a user with a role of Subscriber.

            I thought perhaps it was because Subscriber was set as the default role, but changing that made no difference.

            So, it’s ignoring my edits. Does that make sense? I am using the Members plugin from Memberpress to create my roles. Ideally I want to evaluate the amount and then assign a role based on the amount.

            Thanks for any input you might have.

          2. Note: the code has been updated to support role assignments, so anyone looking can ignore all my examples.