Hooks

Hooks

Here is the list of filters and actions supported by Wise Chat Pro and Wise Chat Live plugins. Please note that any improper usage may lead to unexpected behavior of the chat.

Action: wc_message_inserted
/**
* Fires once a message has been saved.
*
* @since 2.3.2
*
* @param WiseChatMessage $message A message object.
* @param array $attachmentIds Attachment IDs of the uploaded images or files
*/
do_action("wc_message_inserted", $message, $attachmentIds);

// Example:
add_action('wc_message_inserted', function($message, $attachmentIds) {
	error_log('Message posted: ' . $message->getText(). ' on ' . $message->getChannelName() . ' channel');
	if (count($attachmentIds) > 0) {
		error_log('Uploaded files: ');
		foreach ($attachmentIds as $attachmentId) {
			error_log(wp_get_attachment_url($attachmentId));
		}
	}
}, 10, 2);
Action: wc_message_updated
/**
* Fires once a message has been updated.
*
* @since 2.3.2
*
* @param WiseChatMessage $message A message object.
*/
do_action("wc_message_updated", $message);

// Example:
add_action('wc_message_updated', function($message) {
	error_log('Message updated: ' . $message->getText(). ' on ' . $message->getChannelName() . ' channel');
}, 10, 1);
Action: wc_message_deleted
/**
* Fires once a message has been deleted.
*
* @since 2.3.2
*
* @param WiseChatMessage $message A deleted message object.
*/
do_action("wc_message_deleted", $message);

// Example:
add_action('wc_message_deleted', function($message) {
	error_log('Message deleted: ' . $message->getText(). ' on ' . $message->getChannelName() . ' channel');
}, 10, 1);
Action: wc_message_approved
/**
* Fires once a message has been approved.
*
* @since 2.3.2
*
* @param WiseChatMessage $message A message object.
*/
do_action("wc_message_approved", $message);

// Example:
add_action('wc_message_approved', function($message) {
	error_log('Message approved: ' . $message->getText(). ' on ' . $message->getChannelName() . ' channel');
}, 10, 1);
Action: wc_ip_banned
/**
* Fires once IP address has been banned.
*
* @since 2.3.2
*
* @param string $ip Banned IP address
* @param integer $duration Duration of the ban (in seconds)
*/
do_action("wc_ip_banned", $ip, $duration);

// Example:
add_action('wc_ip_banned', function($ip, $duration) {
	error_log('IP address banned: ' . $ip. ', duration: ' . $duration . ' s.');
}, 10, 2);
Action: wc_ip_kicked
/**
* Fires once IP address has been kicked.
*
* @since 2.3.2
*
* @param string $ip Kicked IP address
* @param integer $userName Name of the kicked user
*/
do_action("wc_ip_kicked", $ip, $userName);

// Example:
add_action('wc_ip_kicked', function($ip, $userName) {
	error_log('IP address kicked: ' . $ip. ', user: ' . $userName);
}, 10, 2);
Action: wc_spam_reported
/**
* Fires once a spam message has been reported.
*
* @since 2.3.2
*
* @param WiseChatMessage $message A reported spam message
* @param string $url URL of the chat page
*/
do_action("wc_spam_reported", $message, $url);

// Example:
add_action('wc_spam_reported', function($message, $url) {
	error_log('SPAM message reported: ' . $message->getText(). ', URL: ' . $url);
}, 10, 2);
Action: wc_username_changed
/**
* Fires once user has changed its name. It applies to anonymous users only.
*
* @since 2.3.2
*
* @param string $oldName The old name
* @param string $userName The new name
* @param WiseChatUser $user The user object
*/
do_action("wc_username_changed", $oldName, $userName, $user);

// Example:
add_action('wc_username_changed', function($oldName, $userName, $user) {
	error_log('Username changed from ' . $oldName. ' to ' . $userName . ', chat user ID: ' . $user->getId());
}, 10, 3);
Action: wc_usercolor_set
/**
* Fires once user has set its color.
*
* @since 2.3.2
*
* @param string $color The color code
* @param WiseChatUser $user The user object
*/
do_action("wc_usercolor_set", $color, $user);

// Example:
add_action('wc_usercolor_set', function($color, $user) {
	error_log('User color has been set to ' . $color . ', chat user ID: ' . $user->getId());
}, 10, 2);
Action: wc_userproperty_set
/**
* Fires once user property has been set.
*
* @since 2.3.2
*
* @param string $property Property name
* @param mixed $value Property value
* @param WiseChatUser $user The user object
*/
do_action("wc_userproperty_set", $property, $value, $user);

// Example:
add_action('wc_userproperty_set', function($property, $value, $user) {
	error_log('User property ' . $property . ' has been set to ' . $value . ', chat user ID: ' . $user->getId());
}, 10, 3);
Action: wc_usersetting_set
/**
* Fires once user setting has been set. User settings are stored in cookie only.
*
* @since 2.3.2
*
* @param string $settingName Setting name
* @param mixed $settingValue Setting value
* @param WiseChatUser $user The user object
*/
do_action("wc_usersetting_set", $settingName, $settingValue, $user);

// Example:
add_action('wc_usersetting_set', function($settingName, $settingValue, $user) {
	error_log('User setting ' . $settingName . ' has been set to ' . $settingValue . ', chat user ID: ' . $user->getId());
}, 10, 3);
Action: wc_user_session_started
/**
* Fires once user has started its session in the chat.
*
* @since 2.3.2
*
* @param WiseChatUser $user The user object
*/
do_action("wc_user_session_started", $user);

// Example:
add_action('wc_user_session_started', function($user) {
	if ($user->getWordPressId() > 0) {
		$wpUser = get_user_by('id', $user->getWordPressId());
		error_log('User session started, user e-mail: ' . $wpUser->user_email);
	} else if (strlen($user->getExternalType()) > 0) {
		error_log('User session started, external user ID: ' . $user->getExternalId());
	} else {
		error_log('User session started, anonymous user: ' . $user->getName());
	}
	
}, 10, 1);
Filter: wc_insert_message
/**
* Filters a message just before it is inserted into the database.
*
* @since 2.3.2
*
* @param WiseChatMessage $message A fully prepared message object. Depending on enabled features:
*	- links are converted to internal tags: [link]
*   - image links are downloaded to Media Library and converted to internal tags: [img]
*   - the text of the message is trimmed and filtered using the bad words dictionary
*   - YouTube links are converted to internal tags: [youtube]
*   - images or files attached to the message via the uploader are converted to internal tags: [img] or [attachment]
* @param string $text A raw text of the message typed by chat user
*/
apply_filters('wc_insert_message', $message, $text);

// Example 1: replace the entire text of the message:
add_filter('wc_insert_message', function($message, $text) {
	$message->setText("Replacement text");
	
	return $message;
}, 10, 2);

// Example 2: append some text to the end of the message:
add_filter('wc_insert_message', function($message, $text) {
	$message->setText($message->getText() . " additional text");
	
	return $message;
}, 10, 2);

// Example 3: detecting internal system messages:
add_filter('wc_insert_message', function($message, $text) {
	if ($message->isAdmin()) {
		$message->setText("This is internal message posted by Wise Chat Pro");
	} else {
		$message->setText("This is a message posted by real user");
	}
	
	return $message;
}, 10, 2);

// Example 4: append a link to the end of the message:
add_filter('wc_insert_message', function($message, $text) {
	// Notice: works only with the links enabled in Wise Chat Pro settings
	$message->setText($message->getText() . ' attached link: [link src="https://kainex.pl/"]');
	
	return $message;
}, 10, 2);

// Example 5: append a link to the end of the message:
add_filter('wc_insert_message', function($message, $text) {
	// Notice: works only with the links and images enabled in Wise Chat Pro settings
	$root = site_url();
	$message->setText(
		$message->getText() . 
		' attached image: [img id="12345" src="'.$root.'/wp-admin/images/w-logo-blue.png" src-th="'.$root.'/wp-admin/images/wordpress-logo.png" src-org="'.$root.'/wp-admin/images/w-logo-blue.png"] '
	); // in most cases src attribute equals src-org attribute, src-th attribute points to the image's thumbnail file
	
	return $message;
}, 10, 2);

// Example 6: append YouTube player to the end of the message:
add_filter('wc_insert_message', function($message, $text) {
	// Notice: works only with YouTube videos enabled in Wise Chat Pro settings
	$message->setText($message->getText() . ' attached YT video: [youtube movie-id="HGVJY_wraM8" src-org="https://www.youtube.com/watch?v=HGVJY_wraM8"]');
	
	return $message;
}, 10, 2);

// Example 7: loading and processing the sender
add_filter('wc_insert_message', function($message, $text) {
	$container = Kainex\WiseChatPro\Container::getInstance();

	// get the sender (WiseChatUser object):
	$sender = $container->get(Kainex\WiseChatPro\DAO\User\WiseChatUsersDAO::class)->get($message->getUserId());
	if ($sender !== null) {
		// do something with $sender object, for example:
		$message->setText('Sent by: ' . $sender->getName() . ' from host '.$sender->getIp());
	}
	
	// get the sender (WP_User object):
	// Notice: anonymous and external users (those authenticated via FB / Twitter / Google) do not have a corresponding WP_User object
	$sender = $message->getWordPressUserId() > 0 ? get_user_by('id', $message->getWordPressUserId()) : false;
	if ($sender !== false) {
		// do something with $sender object, for example:
		$message->setText('Sent by: ' . $sender->user_email);
	}
	
	return $message;
}, 10, 2);

// Example 8: detecting private messages
add_filter('wc_insert_message', function($message, $text) {

	// if recipient ID is not null then this is a private message:
	if ($message->getRecipientId() !== null) {
		$container = Kainex\WiseChatPro\Container::getInstance();

		// get the sender (WiseChatUser object):
		$privateMessageSender = $container->get(Kainex\WiseChatPro\DAO\User\WiseChatUsersDAO::class)->get($message->getUserId());
		// get the recipient (WiseChatUser object):
		$privateMessageRecipient = $container->get(Kainex\WiseChatPro\DAO\User\WiseChatUsersDAO::class)->get($message->getRecipientId());
		
		if ($privateMessageSender !== null && $privateMessageRecipient !== null) {
			// do something with the sender and the recipient:
			$message->setText('Sent by: ' . $privateMessageSender->getName() . ' to: '.$privateMessageRecipient->getName());
			
			// get the sender (WP_User object):
			$senderWPUser = $privateMessageSender->getWordPressId() > 0 ? get_user_by('id', $privateMessageSender->getWordPressId()) : false;
			// get the recipient (WP_User object):
			$recipientWPUser = $privateMessageRecipient->getWordPressId() > 0 ? get_user_by('id', $privateMessageRecipient->getWordPressId()) : false;
			
			// notice: anonymous and external users (those authenticated via FB / Twitter / Google) do not have a corresponding WP_User object
			if ($senderWPUser !== false && $recipientWPUser !== false) {
				// do something with the sender and the recipient:
				$message->setText('Sent by: ' . $senderWPUser->user_email . ' to: ' . $recipientWPUser->user_email);
			}
		}
	}
	
	return $message;
}, 10, 2);

// Example 9: using the raw uprocessed text
add_filter('wc_insert_message', function($message, $text) {
	$message->setText("This is the unprocessed text: " . $text);
	
	return $message;
}, 10, 2);

// Example 10: get the channel and the timestamp
add_filter('wc_insert_message', function($message, $text) {
	$message->setText("This was published on " . $message->getChannelName() . ' channel at '.date('Y-m-d H:i:s', $message->getTime()));
	
	return $message;
}, 10, 2);
Filter: wc_chat_js_configuration
/**
 * Filters the configuration of the chat. The configuration is then used in the front-end rendering code.
 *
 * @since 3.5.4
 *
 * @param array $jsOptions Chat's configuration array
 */
$jsOptions = apply_filters('wc_chat_js_configuration', $jsOptions);

// Example:
add_filter('wc_chat_js_configuration', function($data) {
	$data['theme'] = 'clear';
	
	return $data;
}, 10, 1);
Filter: wc_chat_channel_stats_html
/**
* Filters HTML outputted by channel stats shortcode:
* [wise-chat-channel-stats template="Channel: {channel} Messages: {messages} Users: {users}"]
*
* @since 2.3.2
*
* @param string $html A HTML code outputted by channel stats shortcode
* @param WiseChatChannel $channel The channel
*/
apply_filters('wc_chat_channel_stats_html', $html, $channel);

// Example:
add_filter('wc_chat_channel_stats_html', function($html, $channel) {
	$html .= 'Wise Chat Pro';
	
	return $html;
}, 10, 2);
Filter: wc_user_geo_code
/**
* Allows to implement custom geo-coding method. Based on given IP address
* it should either return fully filled WiseChatGeoDetails object or null
* if IP address could not be geo-coded.
*
* @since 2.3.2
*
* @param string $ipAddress
*/
apply_filters('wc_user_geo_code', $ipAddress);

// Example:
add_filter('wc_user_geo_code', function($ipAddress) {
	$details = new WiseChatGeoDetails();

	$details->setCity('New York');
	$details->setCountryCode('US');
	$details->setCountry('United States');
	
	return strlen($ipAddress) > 0 ? $details : null;
}, 10, 1);