The MxChat plugin provides powerful hooks that allow you to customize and filter both incoming prompts and outgoing responses. This system lets you add personalization, timestamps, content filtering, business logic, and much more without modifying the core plugin files.
The filtering system works on two levels:
The filtering system works on two levels:
- Server-side (PHP): Filters incoming prompts before they reach the AI
- Client-side (JavaScript): Filters outgoing responses before they’re displayed to users
Complete Examples
Example 1: User Personalization
What it does: Addresses logged-in users by name and encourages anonymous users to register. Add this to your functions.php file:// Personalize chat responses based on user login status
add_action('wp_head', 'mxchat_user_personalization');
function mxchat_user_personalization() {
?>
<script>
function customMxChatFilter(message, direction) {
if (direction === "response") {
<?php if (is_user_logged_in()): ?>
<?php
$current_user = wp_get_current_user();
$first_name = get_user_meta($current_user->ID, 'first_name', true);
$name = !empty($first_name) ? $first_name : $current_user->display_name;
?>
// Add personalized message for logged-in users
return message + "\n\nAnything else I can help you with, <?php echo esc_js($name); ?>?";
<?php else: ?>
// Encourage registration for anonymous users
return message + '\n\n💡 <em><a href="/wp-login.php?action=register">Create a free account</a> to get personalized responses!</em>';
<?php endif; ?>
}
return message;
}
</script>
<?php
}
Test it: Ask any question and see personalized responses.
Result:
- Logged-in users see: “AI response… Anything else I can help you with, Sarah?”
- Anonymous users see: “AI response… 💡 Create a free account to get personalized responses!” (with clickable link)
Example 2: Professional Disclaimers
What it does: Automatically adds disclaimers when AI mentions medical, legal, or financial topics. Add this to your functions.php file:// Professional compliance and disclaimer system
add_action('wp_head', 'mxchat_compliance_disclaimers');
function mxchat_compliance_disclaimers() {
?>
<script>
function customMxChatFilter(message, direction) {
if (direction === "response") {
const messageLower = message.toLowerCase();
// Add medical disclaimer
if (messageLower.includes('medical') || messageLower.includes('health') ||
messageLower.includes('symptom') || messageLower.includes('treatment') ||
messageLower.includes('doctor') || messageLower.includes('medicine')) {
return message + "\n\n⚠️ <strong>Important</strong>: This is not medical advice. Please consult your doctor or healthcare provider for medical concerns.";
}
// Add legal disclaimer
if (messageLower.includes('legal') || messageLower.includes('law') ||
messageLower.includes('lawsuit') || messageLower.includes('attorney') ||
messageLower.includes('contract') || messageLower.includes('rights')) {
return message + "\n\n⚖️ <strong>Legal Notice</strong>: This is not legal advice. Please consult a qualified attorney for legal matters.";
}
// Add financial disclaimer
if (messageLower.includes('investment') || messageLower.includes('financial') ||
messageLower.includes('tax') || messageLower.includes('stock') ||
messageLower.includes('money') || messageLower.includes('invest')) {
return message + "\n\n💼 <strong>Financial Disclaimer</strong>: This is not financial advice. Consult a financial advisor for investment decisions.";
}
}
return message;
}
</script>
<?php
}
Test it: Ask “What are the symptoms of a cold?” or “How do I invest money?”
Result: The AI responds normally, but automatically adds appropriate disclaimers at the end.
Example 3: Business Hours & Contact Info
What it does: Automatically adds business hours and contact information to relevant responses. Add this to your functions.php file:// Business information enhancement
add_action('wp_head', 'mxchat_business_info');
function mxchat_business_info() {
?>
<script>
function customMxChatFilter(message, direction) {
if (direction === "response") {
const messageLower = message.toLowerCase();
// Add business hours for time-related questions
if (messageLower.includes('hours') || messageLower.includes('open') ||
messageLower.includes('closed') || messageLower.includes('when')) {
return message + "\n\n🕐 <strong>Business Hours</strong>:\nMon-Fri: 9 AM - 6 PM EST\nSat: 10 AM - 4 PM EST\nSun: Closed";
}
// Add contact info for support questions
if (messageLower.includes('contact') || messageLower.includes('support') ||
messageLower.includes('help') || messageLower.includes('phone')) {
return message + '\n\n📞 <strong>Contact Us</strong>:\nPhone: <a href="tel:555-123-4567">555-123-4567</a>\nEmail: <a href="mailto:[email protected]">[email protected]</a>';
}
// Add location info for location questions
if (messageLower.includes('location') || messageLower.includes('address') ||
messageLower.includes('where') || messageLower.includes('visit')) {
return message + "\n\n📍 <strong>Visit Us</strong>:\n123 Main Street\nYour City, State 12345\n🗺️ <a href='https://maps.google.com/?q=123+Main+Street'>Get Directions</a>";
}
}
return message;
}
</script>
<?php
}
Test it: Ask “What are your hours?” or “How do I contact you?” or “Where are you located?”
Result: AI responds normally, then adds relevant business information with clickable phone numbers and links.
Example 4: Content Enhancement for E-commerce
What it does: Adds helpful shopping links when customers ask about products, shipping, or returns. Add this to your functions.php file:// E-commerce enhancement system
add_action('wp_head', 'mxchat_ecommerce_enhancements');
function mxchat_ecommerce_enhancements() {
?>
<script>
function customMxChatFilter(message, direction) {
if (direction === "response") {
const messageLower = message.toLowerCase();
// Add product browsing links
if (messageLower.includes('product') || messageLower.includes('item') ||
messageLower.includes('buy') || messageLower.includes('shop')) {
return message + '\n\n🛍️ <strong>Shop Now</strong>: <a href="/shop">Browse All Products</a> | <a href="/sale">Current Sale Items</a>';
}
// Add shipping information
if (messageLower.includes('shipping') || messageLower.includes('delivery') ||
messageLower.includes('ship') || messageLower.includes('send')) {
return message + '\n\n📦 <strong>Shipping Info</strong>: <a href="/shipping-policy">Shipping Policy</a> | <a href="/track-order">Track Your Order</a>';
}
// Add return policy links
if (messageLower.includes('return') || messageLower.includes('refund') ||
messageLower.includes('exchange') || messageLower.includes('warranty')) {
return message + '\n\n↩️ <strong>Returns</strong>: <a href="/return-policy">Return Policy</a> | <a href="/start-return">Start a Return</a>';
}
// Add account benefits for pricing questions
if (messageLower.includes('price') || messageLower.includes('cost') ||
messageLower.includes('discount') || messageLower.includes('coupon')) {
<?php if (is_user_logged_in()): ?>
return message + '\n\n💰 <strong>Member Benefits</strong>: Check your account for exclusive member discounts!';
<?php else: ?>
return message + '\n\n💰 <strong>Save Money</strong>: <a href="/register">Create an account</a> for member-only discounts and faster checkout!';
<?php endif; ?>
}
}
return message;
}
</script>
<?php
}
Test it: Ask “Do you have any products on sale?” or “What’s your shipping policy?” or “How much does this cost?”
Result: AI responds normally, then adds relevant shopping links and encouragement to create accounts for better deals.
Example 5: Analytics Tracking
What it does: Tracks user questions and sends data to Google Analytics without changing the conversation. Add this to your functions.php file:// Analytics tracking for chat interactions
add_action('wp_head', 'mxchat_analytics_tracking');
function mxchat_analytics_tracking() {
?>
<script>
function customMxChatFilter(message, direction) {
if (direction === "prompt") {
// Track user questions in Google Analytics
if (typeof gtag !== 'undefined') {
// Determine the category of question
const messageLower = message.toLowerCase();
let category = 'general';
if (messageLower.includes('price') || messageLower.includes('cost') || messageLower.includes('buy')) {
category = 'pricing';
} else if (messageLower.includes('shipping') || messageLower.includes('delivery')) {
category = 'shipping';
} else if (messageLower.includes('return') || messageLower.includes('refund')) {
category = 'returns';
} else if (messageLower.includes('support') || messageLower.includes('help')) {
category = 'support';
}
gtag('event', 'chat_question', {
'event_category': 'chatbot',
'event_label': category,
'custom_parameter_1': message.length // Track message length
});
console.log('📊 Tracked chat question:', category);
}
}
return message; // Don't change the message, just track it
}
</script>
<?php
}
Test it: Ask any question and check your browser console for “📊 Tracked chat question:” messages.
Result: All user questions are categorized and sent to Google Analytics for insights, but the conversation remains unchanged.
Simple Server-Side Enhancement
For basic server-side message enhancement:// Add user context to prompts for better AI responses
add_filter('mxchat_filter_message', 'mxchat_add_user_context', 10, 3);
function mxchat_add_user_context($message, $direction, $session_id) {
if ($direction === 'prompt') {
$current_user = wp_get_current_user();
if ($current_user->ID > 0) {
// Add user info so AI can personalize responses
$context = "User: {$current_user->display_name}";
// Add customer order count if WooCommerce is active
if (function_exists('wc_get_customer_order_count')) {
$order_count = wc_get_customer_order_count($current_user->ID);
$context .= ", Previous Orders: {$order_count}";
}
return "{$context} | Question: {$message}";
}
}
return $message;
}
Test it: Ask any question while logged in. The AI will receive context about who you are.
Result: AI responses become more personalized because it knows your name and order history.
Best Practices
Testing Your Filters
- Start simple: Add
console.log('Filter running:', direction, message);to see if it’s working - Test both directions: Try questions (prompts) and check responses
- Test different users: Try while logged in and logged out
- Check browser console: Look for JavaScript errors
Performance Tips
- Keep filter functions lightweight
- Avoid heavy database queries
- Don’t add too many conditions in one filter
Function Naming
- Always name your JavaScript function exactly
customMxChatFilter - Use descriptive PHP function names
- Add comments explaining what each section does
Troubleshooting
Filter not working at all:- Ensure function is named exactly
customMxChatFilter - Check browser console for JavaScript errors
- Verify code was added to functions.php correctly
- Clear any caching plugins
- Try in an incognito browser window
- Check that you’re testing the right direction (prompt vs response)
- Use HTML
<a href="">tags, not markdown[text](url) - The filter runs after WordPress processes content