← Back to WordPress Plugin Documentation

WordPress Hooks & Filters Reference

Developer Customization Guide (v1.1.0+)

Last Updated: January 8, 2026

Starting with SokutoAI WordPress plugin v1.1.0, you can customize the chatbot behavior using WordPress's standard hooks and filters system. Add these code snippets to your theme's functions.php or a custom plugin.

What You Can Do

Filters

sokuto_ai_widget_enabled

Control chatbot visibility on a per-page basis.

Parameters:

  • $enabled(bool) - Whether to display the widget (default: true)
  • $post_id(int|false) - Current post ID (false if not available)

Hide on Privacy Policy page:

add_filter( 'sokuto_ai_widget_enabled', function( $enabled, $post_id ) {
    if ( is_page( 'privacy-policy' ) ) {
        return false;
    }
    return $enabled;
}, 10, 2 );

sokuto_ai_system_prompt

Pass additional context information to the AI. You can dynamically provide product details, page content, or other information for the AI to reference.

Parameters:

  • $context(string) - Current context (default: empty string)
  • $post_id(int|false) - Current post ID
  • $post_type(string|false) - Current post type

Pass WooCommerce product info to AI:

add_filter( 'sokuto_ai_system_prompt', function( $context, $post_id, $post_type ) {
    if ( $post_type === 'product' && function_exists( 'wc_get_product' ) ) {
        $product = wc_get_product( $post_id );
        if ( $product ) {
            $context .= "\n\n[Current Product]\n";
            $context .= "Name: " . $product->get_name() . "\n";
            $context .= "Price: $" . number_format( $product->get_price(), 2 ) . "\n";
            $context .= "Stock: " . ( $product->is_in_stock() ? 'In stock' : 'Out of stock' );
        }
    }
    return $context;
}, 10, 3 );

sokuto_ai_welcome_message

Customize the chat widget's initial message on a per-page basis.

Parameters:

  • $message(string) - Welcome message (default: empty string)
  • $post_id(int|false) - Current post ID

Set different welcome messages per page:

add_filter( 'sokuto_ai_welcome_message', function( $message, $post_id ) {
    if ( function_exists( 'is_cart' ) && is_cart() ) {
        return 'Have questions about payment methods?';
    }
    if ( is_page( 'contact' ) ) {
        return 'I might be able to help before you contact us!';
    }
    return $message;
}, 10, 2 );

sokuto_ai_user_context

Add logged-in user information to the AI context. Please be mindful of privacy and include only essential information.

Parameters:

  • $user_context(array) - User context array (default: empty array)
  • $user_id(int) - User ID (0 if not logged in)

Pass member information to AI:

add_filter( 'sokuto_ai_user_context', function( $context, $user_id ) {
    if ( $user_id === 0 ) {
        $context['is_logged_in'] = false;
        return $context;
    }
    $user = get_user_by( 'ID', $user_id );
    $context['is_logged_in'] = true;
    $context['display_name'] = $user->display_name;

    // For WooCommerce customers
    if ( function_exists( 'wc_get_customer_order_count' ) ) {
        $context['order_count'] = wc_get_customer_order_count( $user_id );
    }
    return $context;
}, 10, 2 );

sokuto_ai_script_attributes

Customize data attributes added to the widget script tag. Use this to dynamically set language, theme color, or other settings.

Parameters:

  • $attributes(array) - Attribute array ['data-xxx' => 'value']

Set language and theme color dynamically:

add_filter( 'sokuto_ai_script_attributes', function( $attributes ) {
    $attributes['data-lang'] = get_locale() === 'ja' ? 'ja' : 'en';
    $attributes['data-theme-color'] = get_theme_mod( 'primary_color', '#3B82F6' );
    return $attributes;
} );

Actions

sokuto_ai_on_chat_open

Execute JavaScript when the chat widget is opened. Useful for analytics tracking.

Track chat opens with GA4:

add_action( 'sokuto_ai_on_chat_open', function() {
    ?>
    if (typeof gtag !== 'undefined') {
        gtag('event', 'chat_opened', {
            'event_category': 'engagement'
        });
    }
    <?php
} );

sokuto_ai_on_message_sent

Execute JavaScript when a user sends a message. The variable 'message' contains the sent content.

Log sent messages:

add_action( 'sokuto_ai_on_message_sent', function() {
    ?>
    console.log('User sent:', message);
    <?php
} );