Last active
December 4, 2023 20:57
-
-
Save fahdi/795fa2f4800dd3e44a908eea364c5093 to your computer and use it in GitHub Desktop.
Add Ultimate Member user to The Newsletter plugin on registration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'um_after_save_registration_details', 'my_after_save_registration_details', 10, 2 ); | |
function my_after_save_registration_details( $user_id, $submitted ) { | |
if ( isset( $submitted['newsletter_subscription'] ) && $submitted['newsletter_subscription'][0] === 'Yes' ) { | |
// Check if the user is not already subscribed | |
// Call the my_subscribe function to create a subscription | |
$subscription_result = my_subscribe( $submitted ); | |
if ( is_wp_error( $subscription_result ) ) { | |
// Handle subscription error if needed | |
error_log( 'Error creating subscription for user with ID ' . $user_id ); | |
} else { | |
error_log( 'User with ID ' . $user_id . ' subscribed successfully.' ); | |
} | |
} | |
} | |
function my_subscribe( $submitted ) { | |
// Example: You can extract the email from the submitted data | |
$user_email = isset( $submitted['user_email'] ) ? $submitted['user_email'] : ''; | |
$first_name = isset( $submitted['first_name'] ) ? $submitted['first_name'] : ''; | |
$last_name = isset( $submitted['last_name'] ) ? $submitted['last_name'] : ''; | |
// You can also modify this function to set other subscription details | |
// based on the submitted data | |
// Create a subscription object and set its properties | |
$subscription = NewsletterSubscription::instance()->get_default_subscription(); | |
$subscription->data->email = $user_email; | |
$subscription->data->name = $first_name; | |
$subscription->data->surname = $last_name; | |
$subscription->optin = 'single'; // Or 'double' based on your requirements | |
// Call the subscribe2 method to create the subscription | |
$result = NewsletterSubscription::instance()->subscribe2( $subscription ); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment