I was wondering to know if it is possible to run a WordPress function with a delay of some seconds or minutes.
I use the following snippet to send an email to admin after a new user is registered on my website
add_action( 'user_register', 'registration_email_to_admin', 10, 1 );
function registration_email_to_admin( $user_id ) {
wp_new_user_notification( $user_id );
}
Then I use the following code to filter the email's content
//Filters the contents of the new user notification email sent to the site admin.
add_filter( 'wp_new_user_notification_email_admin', 'custom_wp_new_user_notification_admin_email', 10, 3 );
function custom_wp_new_user_notification_admin_email( $wp_new_user_notification_email, $user, $blogname ) {
//pass user id into a variable
$user_id = $user->id;
//get teams for that user
$teams = wc_memberships_for_teams_get_teams( $user_id );
$teamNames = [];
foreach($teams as $teamName){
array_push($teamNames, $teamName->get_name());
}
$teamNamesString = implode (',' , $teamNames);
$wp_new_user_notification_email['subject'] = sprintf( '[%s] New user registered. User ID = %s', $blogname, $user->id );
$wp_new_user_notification_email['message'] = sprintf( "%s ( %s ) has registered to the website %s.", $user->user_login, $user->user_email, $blogname ) . "\r\n\r\n";
$wp_new_user_notification_email['message'] .= sprintf(__('Username: %s'), $user->user_login) . "\r\n";
$wp_new_user_notification_email['message'] .= sprintf(__('Email: %s'), $user->user_email) . "\r\n";
$wp_new_user_notification_email['message'] .= sprintf(__('ID: %s'), $user->id) . "\r\n";
$wp_new_user_notification_email['message'] .= sprintf(__('Team Name: %s'), $teamNamesString) . "\r\n";
$wp_new_user_notification_email['headers'] = "From: Members Website <info@test.com> \n\r cc: GeorgeFR <test@yahoo.com>";
return $wp_new_user_notification_email;
}
On all the above, everything works well except the last append to the message
$wp_new_user_notification_email['message'] .= sprintf(__('Team Name: %s'), $teamNamesString) . "\r\n";
Using the WooCommerce Memberships for Teams plugin, it seems that the new user is being assigned to the team that has been invited to after his/her registration. That being said, the variable $teamNamesString
is always empty on my email content.
So, is it possible to run all the above with some delay, making sure that the team has been assigned to the new user before I send the email? It is important for me to include the team's name to the email for business management purposes.