Closed gpoole closed 4 years ago
@gpoole I don't think this is something that this plugin should handle because it lies outside the scope of its core task. It's very easy to implement, however, so I'll leave the basics here for people to refer to:
// Act when CiviCRM WordPress Member Sync creates a user.
add_action( 'civi_wp_member_sync_after_insert_user', 'my_notification_send_to_user', 10, 2 );
/**
* Send a notification to a WordPress user when they are created.
*
* Users are created by the CiviCRM WordPress Member Sync plugin.
*
* @param array $contact The array of CiviCRM contact data.
* @param int $user_id The numeric ID of the WordPress user.
*/
function my_notification_send_to_user( $contact, $user_id ) {
// Filter the notification email(s).
add_filter( 'wp_new_user_notification_email', 'my_notification_filter_user', 10, 3 );
// Send only to the new user.
wp_new_user_notification( $user_id, null, 'user' );
}
/**
* Filters the content of the new user notification email sent to the new user.
*
* This filter is available since WordPress 4.9.0.
*
* @param array $notification_email_user {
* Used to build wp_mail().
* @type string $to The intended recipient - New user email address.
* @type string $subject The subject of the email.
* @type string $message The body of the email.
* @type string $headers The headers of the email.
* }
* @param WP_User $user The user object for new user.
* @param string $blogname The site title.
* @return array $wp_new_user_notification_email The modified data used to build wp_mail().
*/
function my_notification_filter_user( $notification_email_user, $user, $blogname ) {
// Code to customise your email goes here.
// --<
return $notification_email_user;
}
Hope that helps.
Thanks for the code sample. I'm wondering though, without sending the email it seems to me there's no way for the created users to log in to the accounts this plugin creates? Is there another more correct way to do this or do you mean the intended usage is to use the hook?
@gpoole Yes, use the hook.
Users can also go to the "Lost password" screen, enter their email address and reset their password to login - and since there are multiple workflows, I consider it outside the scope of this plugin.
Hi there,
Thanks for your work on this. I was a little confused when I first found this plugin about how newly created users could set up their accounts, but from checking the code seems like this plugin isn't currently notifying newly created users of their login details. Should this plugin maybe support optionally calling
wp_new_user_notification
, or is there another way to handle users setting their passwords? If there is a better way, could you please clarify how that works in the readme?Thanks!