Using Filters in Init Chat Engine

The Init Chat Engine plugin is not just a simple realtime chat widget — it’s built as an extensible platform where developers can modify behavior through filters and actions. With these hooks, you can inject custom metadata into messages, override how the word filter works, add VIP or profile logic, or extend the frontend UI without touching the core plugin code. This article provides practical, copy-ready examples that you can drop into your theme or custom plugin to customize Init Chat Engine exactly the way you want.

Using Filters in Init Chat Engine

Init Chat Engine exposes a set of filters/actions that allow theme and plugin developers to modify message processing, extend data returned via REST API, or even override how the chat behaves on the frontend. All examples below can be placed in functions.php (theme) or in your custom plugin.

1. Change word filtering strategy

Use this filter to control how blocked words are matched. Available strategies: substring (default), word, regex.

// Force strategy to 'word' matching
add_filter(
    'init_plugin_suite_chat_engine_word_filter_strategy',
    function ( $strategy, $settings, $message ) {
        return 'word'; // or 'substring' or 'regex'
    },
    10,
    3
);

2. Append blocked words programmatically

Adds extra blocked words even if the admin didn’t configure them in the UI.

add_filter(
    'init_plugin_suite_chat_engine_blocked_words',
    function ( $words, $settings, $message ) {
        $words[] = 'https://';
        $words[] = 'discord.gg';
        $words[] = 't.me';
        return array_values( array_unique( $words ) );
    },
    10,
    3
);

3. Bypass filter for moderators or VIP users

Allows specific users to send messages without being filtered.

add_filter(
    'init_plugin_suite_chat_engine_bypass_filter',
    function ( $bypass, $message, $user, $settings ) {
        if ( $user instanceof WP_User ) {
            if ( user_can( $user, 'moderate_comments' ) ) {
                return true;
            }
            $vip = get_user_meta( $user->ID, 'ice_vip', true );
            if ( (int) $vip === 1 ) {
                return true;
            }
        }
        return $bypass;
    },
    10,
    4
);

4. Log blocked message events

Useful when monitoring moderation or debugging user-generated content.

add_action(
    'init_plugin_suite_chat_engine_word_block_hit',
    function ( $blocked_word, $raw_message, $strategy ) {
        if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
            error_log(
                sprintf(
                    '[ICE] Blocked | word="%s" | strategy=%s | msg="%s"',
                    $blocked_word,
                    $strategy,
                    wp_strip_all_tags( $raw_message )
                )
            );
        }
    },
    10,
    3
);

5. Enrich message JSON response (Add profile URL, VIP flag, roles)

This runs on the backend before JSON is returned. Use it to enrich message data with user metadata.

add_filter(
    'init_plugin_suite_chat_engine_enrich_message_row',
    function ( $row, $user ) {

        if ( $user instanceof WP_User ) {
            $row['roles']       = array_values( (array) $user->roles );
            $row['is_admin']    = user_can( $user, 'manage_options' ) ? 1 : 0;
            $row['vip']         = (int) get_user_meta( $user->ID, 'ice_vip', true );

            // Example: WordPress author URL as profile link
            $row['profile_url'] = get_author_posts_url( $user->ID );
        } else {
            $row['roles']       = [];
            $row['is_admin']    = 0;
            $row['vip']         = 0;
            $row['profile_url'] = '';
        }

        return $row;
    },
    10,
    2
);

6. Frontend enhancement: Clickable username linked to profile

Runs on the browser whenever a new message is rendered. No core changes required.

// USER PROFILE INIT CHAT ENGINE
window.initChatEngineMessageElementHook = function (el, msg, isCurrentUser) {
    if (!msg) return;

    // Link username to profile_url (if provided by backend)
    if (msg.profile_url) {
        const nameEl = el.querySelector('.init-chatbox-author');
        if (nameEl && nameEl.tagName !== 'A') {
            const a = document.createElement('a');
            a.href = msg.profile_url;
            a.rel = 'noopener';
            a.className = nameEl.className + ' uk-link-reset';
            a.textContent = nameEl.textContent;
            nameEl.replaceWith(a);
        }
    }
};

7. Enable regex mode globally (Advanced)

Useful when handling complex spam patterns.

add_filter(
    'init_plugin_suite_chat_engine_word_filter_strategy',
    fn( $strategy, $settings, $message ) => 'regex',
    10,
    3
);

add_filter(
    'init_plugin_suite_chat_engine_blocked_words',
    function ( $words ) {
        return [
            '/https?:\/\/[^\s]+/iu',
            '/\b(discord\.gg|t\.me)\b/iu'
        ];
    },
    10,
    3
);

Comments


  • No comments yet.

Web-Based Tools

Press Ctrl + \ on desktop, or swipe left anywhere on mobile.

Login