Avoid direct use of wp_new_user_notification
I was taking a look at https://github.com/civicrm/civicrm-core/pull/18982/ and noticed that CiviCRM currently calls wp_new_user_notification
directly. Whilst this is not considered a private function in WordPress, I do believe its not best practice to call it directly.
The problem
wp_new_user_notification
is a very opinionated function. A text-only email is sent via wp_mail
with fixed (albeit translatable) text.
Site owners may want to replace this functionality:
- To disable new-user emails altogether
- To send a branded HTML email
- To send the email via an email service provider (MailChimp, DotDigital etc)
On sites I work on, we frequently replace the wp_new_user_notification with a more heavily branded custom email using code similar to this:
remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
add_action( 'register_new_user', 'my_custom_new_user_notification' );
remove_action( 'edit_user_created_user', 'wp_send_new_user_notifications' );
add_action( 'edit_user_created_user', 'my_custom_new_user_notification' );
(Note slightly different actions would be needed to fully handle a multisite context).
These actions can also be used to trigger other functionality unrelated to email notifications.
The solution
I would like to propose that CiviCRM replaces its direct call to wp_new_user_notification
with a do_action()
call, allowing the notification to be unhooked or replaced. The question in my mind is what action should be used. The options are:
register_new_user
edit_user_created_user
- A new CiviCRM specific action. (The
wp_send_new_user_notifications
function would then need to be attached to this action withadd_action
)
I don't have a particular preference either way on this last point.