Skip to content

Instantly share code, notes, and snippets.

@annuman97
Last active February 2, 2026 09:01
Show Gist options
  • Select an option

  • Save annuman97/70f913a220a86c12c4d1ef65e2b87a3c to your computer and use it in GitHub Desktop.

Select an option

Save annuman97/70f913a220a86c12c4d1ef65e2b87a3c to your computer and use it in GitHub Desktop.
This snippet automatically sends a private welcome message from an admin to every newly registered user in FluentCommunity (using the FluentCommunity Chat add-on). It works around the current limitation where members cannot initiate private messages with admins only. Instead, the admin initiates the conversation at registration time, allowing th…
<?php
/**
* Auto Welcome DM from Admin to New User (FluentCommunity Chat)
*/
defined('ABSPATH') || exit;
add_action('user_register', function ($user_id) {
// ---- CHANGE THESE TWO VALUES ----
$admin_user_id = 1; // 👈 আপনার ADMIN user ID
$welcome_text = 'Hi 👋 Welcome to the community!
If you need any help or have questions, just reply to this message.';
//You can change this welcome message as well
// --------------------------------
// Make sure FluentCommunity Chat is active
if (
!class_exists('\FluentMessaging\App\Services\ChatHelper') ||
!class_exists('\FluentMessaging\App\Models\Thread') ||
!class_exists('\FluentMessaging\App\Models\Message')
) {
return;
}
$user_id = (int) $user_id;
$admin_user_id = (int) $admin_user_id;
if (!$user_id || !$admin_user_id || $user_id === $admin_user_id) {
return;
}
// Format message like the plugin does
$welcome_text = sanitize_textarea_field($welcome_text);
$welcome_text = str_replace(PHP_EOL, '<br />', $welcome_text);
// Check if a 1:1 thread already exists
$thread = \FluentMessaging\App\Services\ChatHelper::getUserToUserThread(
$user_id,
$admin_user_id
);
// Create thread if not exists
if (!$thread) {
$user = get_user_by('ID', $user_id);
$admin = get_user_by('ID', $admin_user_id);
$thread = \FluentMessaging\App\Models\Thread::create([
'title' => 'Chat between ' .
($user ? $user->display_name : 'User') .
' & ' .
($admin ? $admin->display_name : 'Admin'),
'status' => 'active'
]);
$thread->users()->attach([$user_id, $admin_user_id]);
}
// Send message from Admin
\FluentMessaging\App\Models\Message::create([
'thread_id' => $thread->id,
'user_id' => $admin_user_id,
'text' => $welcome_text
]);
}, 20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment