forked from awesomemotive/wp-simple-pay-snippet-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-wp-user.php
75 lines (57 loc) · 2.63 KB
/
create-wp-user.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* Plugin Name: WP Simple Pay - Create WP User
* Plugin URI: https://wpsimplepay.com
* Description: Create a user using the email address for the payment for one-time and subscription charges.
* Version: 1.0
*/
/**
* In this example, we'll see how to create a WordPress user using the email address entered for the payment.
*
* After the payment is created this code will create a WordPress user with a generated password (using native
* WordPress functions) and then send a basic email to the user's email address with the password that was generated.
*/
// This function controls adding the user for a one-time payment.
function simpay_custom_create_user_from_charge( $charge ) {
// Grab the email from the charge object
$email_address = $charge->receipt_email;
// If a user doesn't exist with this email then create a new user and password
if ( null == username_exists( $email_address ) ) {
// Generate the password and create the user
$password = wp_generate_password( 16, true );
$user_id = wp_create_user( $email_address, $password, $email_address );
// Set the required nickname field to the email address
wp_update_user( array(
'ID' => $user_id,
'nickname' => $email_address,
) );
// Set the role to the lowest role (subscriber). You can change this according to what you need.
$user = new WP_User( $user_id );
$user->set_role( 'subscriber' );
// Email the user the generated password.
wp_mail( $email_address, 'Welcome!', 'Your Password: ' . $password );
}
}
add_action( 'simpay_charge_created', 'simpay_custom_create_user_from_charge' );
// This function controls adding a user for subscription payments.
function simpay_custom_create_user_from_subscription( $charge, $customer ) {
// Grab the email from the customer object
$email_address = $customer->email;
// If a user doesn't exist with this email then create a new user and password
if ( null == username_exists( $email_address ) ) {
// Generate the password and create the user
$password = wp_generate_password( 16, true );
$user_id = wp_create_user( $email_address, $password, $email_address );
// Set the required nickname field to the email address
wp_update_user( array(
'ID' => $user_id,
'nickname' => $email_address,
) );
// Set the role to the lowest role (subscriber). You can change this according to what you need.
$user = new WP_User( $user_id );
$user->set_role( 'subscriber' );
// Email the user the generated password.
wp_mail( $email_address, 'Welcome!', 'Your Password: ' . $password );
}
}
add_action( 'simpay_subscription_created', 'simpay_custom_create_user_from_subscription', 10, 2 );