JGChat is a WordPress plugin that integrates Anthropic's Claude AI into WordPress websites, allowing site owners to provide intelligent chatbot functionality to their visitors. This document outlines the technical architecture, requirements, and implementation details for developers who want to understand, maintain, or extend the plugin.
-
WordPress Plugin Structure
- Main plugin file:
jgchat-plugin.php- Contains core plugin functionality, admin menu registration, database setup, and AJAX handlers - JavaScript:
js/jgchat.js- Frontend functionality, chat UI interaction, and AJAX communication - Stylesheet:
css/jgchat.css- Dark-themed styling based on the JG Widget Style Guide
- Main plugin file:
-
Database
- Table:
{prefix}_jgchat_logs- Stores user questions for analytics and review
- Table:
-
External Dependencies
- Anthropic Claude API - Used for the AI functionality
- Marked.js - For Markdown parsing in chat messages
-
Chat Interface
- Two presentation modes:
- Floating widget (bottom right of screen)
- Embedded chat via shortcode
[jgchat]
- Message types supported:
- Plain text
- Markdown formatting
- Code blocks
- Lists
- Links (auto-detected and made clickable)
- Two presentation modes:
-
Conversation Flow
- Welcome message on initialization
- Messages sent via AJAX to WordPress backend
- Chat history maintained within session
- Typing animation for AI responses
- Scrollable message container
-
Admin Settings
- Chatbot name
- Welcome message
- Input placeholder text
- Claude API key
- Model selection with dynamic refresh:
- Refresh button to fetch latest available Claude models from Anthropic API
- Models stored in WordPress options table
- Dropdown displays full model IDs (e.g., claude-3-7-sonnet-20250219)
- Shows model descriptions and "latest" tags where applicable
- Automatically filters out deprecated models
- Knowledge base content input
- Toggle for enabling/disabling the footer widget
-
Analytics & Logging
- Log of all user questions
- Admin interface to review logs
- Search functionality
- CSV export capability
- Bulk log deletion
-
Admin Interface
- Custom admin menu page for settings
- Custom admin menu page for logs
- Settings API integration for storing configuration
- Custom WP_List_Table implementation for logs display
-
Database Handling
- Uses
dbDelta()for table creation/updates - Simple schema: ID, question text, timestamp
- Query capability for log retrieval
- Uses
-
WordPress Hooks
admin_menu- Registers admin pagesadmin_init- Registers settingswp_enqueue_scripts- Loads assetswp_ajax_jgchat&wp_ajax_nopriv_jgchat- AJAX handlerswp_footer- Adds chat widget
-
Initialization
- DOM-ready event handler
- Welcomes message display
- Widget state management using localStorage
-
UI Components
- Chat message container
- Input field
- Send button
- Typing indicator
- Widget toggle
-
Message Handling
- Formats messages with Markdown support
- Animates AI response typing
- Maintains chat history
- Handles code block rendering
- Auto-detects and formats links
-
AJAX Communication
- Sends user messages to WordPress backend
- Receives AI responses
- Uses WP nonce for security
- Handles errors gracefully
-
API Communication
- HTTP POST to Anthropic's Messages API endpoint
- Headers include API key and version
- Request body includes:
- Selected model
- Chat history
- System message with knowledge base
- Max tokens parameter
-
Response Parsing
- Extracts text content from structured API response
- Handles error conditions
- Stores successful responses in chat history
-
Theming
- Dark theme per JG Widget Style Guide
- Color variables for primary colors, status colors, and text colors
- Responsive design accommodations
-
Components
- Message bubbles
- Input container
- Typing indicator with animation
- Widget button with hover effects
- Modal container for widget
-
Animations
- Message appear animation
- Typing indicator
- Widget button hover
- Smooth scrolling
-
Optimizations
- Minimal DOM manipulation
- Throttled animations
- Efficient AJAX payloads
-
Limitations
- API timeouts set to 30 seconds
- Maximum token count set to 1024
-
Data Protection
- API key stored securely in WordPress options
- AJAX nonce verification
- Input sanitization
- Output escaping
-
Error Handling
- Graceful failure for API errors
- User feedback for connection issues
- Error logging for debugging
Developers can extend JGChat through the following methods:
-
WordPress Filters (to be implemented)
jgchat_system_message- Modify the system message sent to Claudejgchat_api_response- Process API responses before displayjgchat_message_format- Custom message formattingjgchat_widget_enabled- Conditional widget display
-
WordPress Actions (to be implemented)
jgchat_before_send- Fires before sending message to APIjgchat_after_response- Fires after receiving a responsejgchat_log_question- Fires when logging a question
-
CSS Customization
- Target specific selectors for styling modifications
- Override default styles with higher specificity
-
Planned Enhancements
- Message attachments support
- User avatar customization
- Theme selection (light/dark mode)
- Advanced analytics
- Multi-model switching
- Rate limiting
- User feedback collection
-
Technical Debt Items
- Add proper unit tests
- Implement template system for flexibility
- Create developer hooks as mentioned above
- Optimize JavaScript bundle
- Add fallback for browsers without localStorage
-
Requirements
- WordPress 5.6+
- PHP 7.4+
- MySQL 5.6+
- Anthropic API key for testing
-
Local Setup
- WordPress development environment (Local, XAMPP, etc.)
- Plugin installed to
wp-content/plugins/jgchat/ - API key configuration
-
Testing
- Test with multiple WordPress versions
- Test with multiple PHP versions
- Test in different browsers
- Test responsive design
- Validate security measures
jgchat_name- Chatbot namejgchat_welcome- Welcome messagejgchat_placeholder- Input placeholderjgchat_api_key- Claude API keyjgchat_model- Selected Claude modeljgchat_knowledge_base- Custom knowledge contentjgchat_widget_enabled- Widget visibility toggle
CREATE TABLE {prefix}_jgchat_logs (
id bigint(20) NOT NULL AUTO_INCREMENT,
question text NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
)The plugin uses the Claude Messages API:
- Endpoint:
https://api.anthropic.com/v1/messages - Method: POST
- Authentication: API key in header
- Request format:
{ "model": "claude-3-5-sonnet-20241022", "messages": [ {"role": "user", "content": "Hello!"}, {"role": "assistant", "content": "Hi there! How can I help you today?"}, {"role": "user", "content": "What's the capital of France?"} ], "system": "You are JGChat, an AI assistant. Use this knowledge to help answer questions: ...", "max_tokens": 1024 }
- Register the setting in
jgchat_register_settings() - Add the field to the settings form in
jgchat_settings_page() - Access the setting with
get_option('setting_name')
- Update CSS in
css/jgchat.css - For structural changes, modify the message creation in
js/jgchat.js
- Modify database schema in
jgchat_install() - Update log insertion in AJAX handler
- Extend WP_List_Table implementation for display
- Modify the
jgchat_shortcode()function to accept and process attributes - Update the UI creation based on those attributes