← WordPress プラグイン ドキュメントに戻る

WordPress フック/フィルター リファレンス

開発者向けカスタマイズガイド(v1.1.0+)

最終更新日: 2026年1月8日

SokutoAI WordPressプラグイン v1.1.0 以降では、WordPress の標準的なフック・フィルターシステムを使ってチャットボットの動作をカスタマイズできます。これらのコードは theme の functions.php または独自プラグインに追加してください。

こんなことができます

フィルター

sokuto_ai_widget_enabled

ページごとにチャットボットの表示/非表示を制御します。

Parameters:

  • $enabled(bool) - 表示するかどうか(デフォルト: true)
  • $post_id(int|false) - 現在の投稿ID(存在しない場合は false)

プライバシーポリシーページで非表示:

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

AIに追加のコンテキスト情報を渡します。商品情報やページの内容など、AIが参照できる情報を動的に設定できます。

Parameters:

  • $context(string) - 現在のコンテキスト(デフォルト: 空文字列)
  • $post_id(int|false) - 現在の投稿ID
  • $post_type(string|false) - 現在の投稿タイプ

WooCommerce商品情報を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【現在閲覧中の商品】\n";
            $context .= "商品名: " . $product->get_name() . "\n";
            $context .= "価格: ¥" . number_format( $product->get_price() ) . "\n";
            $context .= "在庫: " . ( $product->is_in_stock() ? 'あり' : 'なし' );
        }
    }
    return $context;
}, 10, 3 );

sokuto_ai_welcome_message

チャットウィジェットの初期メッセージをページごとにカスタマイズできます。

Parameters:

  • $message(string) - ウェルカムメッセージ(デフォルト: 空文字列)
  • $post_id(int|false) - 現在の投稿ID

ページ別にウェルカムメッセージを設定:

add_filter( 'sokuto_ai_welcome_message', function( $message, $post_id ) {
    if ( function_exists( 'is_cart' ) && is_cart() ) {
        return 'お支払い方法についてご不明な点はありますか?';
    }
    if ( is_page( 'contact' ) ) {
        return 'お問い合わせの前に、こちらでお答えできるかもしれません!';
    }
    return $message;
}, 10, 2 );

sokuto_ai_user_context

ログインユーザーの情報をAIコンテキストに追加します。プライバシーに配慮し、必要最小限の情報のみ含めてください。

Parameters:

  • $user_context(array) - ユーザーコンテキスト配列(デフォルト: 空配列)
  • $user_id(int) - ユーザーID(未ログインの場合は 0)

会員情報を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;

    // WooCommerce会員の場合
    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

ウィジェットスクリプトタグに追加するdata属性をカスタマイズします。言語設定やテーマカラーなどを動的に設定できます。

Parameters:

  • $attributes(array) - 属性の配列 ['data-xxx' => 'value']

言語とテーマカラーを動的に設定:

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;
} );

アクション

sokuto_ai_on_chat_open

チャットウィジェットが開かれたときにJavaScriptを実行します。アナリティクスのトラッキングなどに利用できます。

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

ユーザーがメッセージを送信したときにJavaScriptを実行します。変数 message に送信内容が格納されています。

送信メッセージをログに出力:

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