AI Financial Calculator
WordPress plugin with financial calculations and AI-powered assistance. Demonstrates OpenAI API integration, amortization calculations, and interactive UX.
by Trygve "Trig" Bundgaard (aka Ubuntu Buddha) · github.com/ubuntu-buddha/ai-financial-calculator
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/ubuntu-buddha/ai-financial-calculator/archive/refs/heads/main.zipAuthor: Trygve "Trig" Bundgaard (aka Ubuntu Buddha)
A production-style WordPress plugin demonstrating financial calculations with AI-powered assistance. Built as a portfolio project showcasing OpenAI API integration, complex financial modeling, and interactive user interfaces in a WordPress environment.
Design Philosophy
This plugin showcases production-ready WordPress development with modern patterns:
- AI Integration: Seamless OpenAI API integration with context-aware responses
- Financial Domain Knowledge: Real-world amortization, ROI calculations, and multi-lender comparisons
- Interactive UX: Real-time calculations, form state persistence, and conversational AI
- WordPress Best Practices: REST API endpoints, shortcodes, proper sanitization, and security
Why This Exists
Financial calculators require:
- Complex mathematical calculations (amortization, interest rate modeling)
- Multi-criteria decision making (credit scores, down payments, terms)
- Revenue/profit projections with multiple variables
- User-friendly interfaces that guide decision-making
- AI assistance for personalized recommendations
This plugin demonstrates how to build such a system with WordPress, integrating external AI APIs while maintaining security and performance.
Architecture
graph TB
A[WordPress Shortcode] -->|Renders| B[Form Template]
B -->|Submits| C[Form Processor]
C -->|Calls| D[Loan Calculator]
C -->|Calls| E[Revenue Calculator]
C -->|Calls| F[Lender Calculators]
C -->|Returns| B
B -->|Displays| G[Results Section]
G -->|User Query| H[AI Chat Interface]
H -->|POST| I[REST API Endpoint]
I -->|Calls| J[OpenAI API]
J -->|Returns| I
I -->|JSON| H
H -->|Renders| K[Markdown Response]
style I fill:#e1f5ff
style J fill:#fff4e1
style C fill:#ffe1e1
style H fill:#e1ffe1
Core Components
-
Financial Calculators (
includes/)- Loan amortization with configurable interest rates
- Revenue/profit projections with expense modeling
- Multi-lender payment comparisons
- ROI and payback period calculations
-
AI Integration (
includes/chat-endpoint.php)- WordPress REST API endpoint
- OpenAI GPT-4o-mini integration
- Context-aware system prompts
- Form data injection for personalized responses
-
Frontend (
assets/)- Vanilla JavaScript (no framework dependencies)
- localStorage for form persistence
- Markdown rendering for AI responses
- Responsive design with CSS Grid
-
Form Processing (
includes/form-processor.php)- Multi-criteria interest rate calculation
- Fee calculations (origination, documentation, servicing)
- Revenue/expense aggregation
- Loan repayment simulation
Key Features
Financial Calculations
- Amortization: Standard loan payment calculations with extra payment support
- Interest Rate Modeling: Multi-factor interest rate calculation based on:
- Credit score ranges
- Down payment percentages
- Loan terms
- Business experience
- Risk factors (age verification, credit penalties)
- Revenue Projections: Daily/monthly/yearly profit calculations
- Expense Modeling: Spoilage, credit card processing, commissions, operating expenses
AI Assistant
- Context-Aware: Receives full form data and calculation results
- Personalized Recommendations: Analyzes user's financial situation
- Markdown Support: Rich text responses with formatting
- Error Handling: Graceful degradation if API unavailable
User Experience
- Form Persistence: Saves form state to localStorage
- Real-Time Calculations: Instant results on form submission
- Multi-Lender Comparison: Side-by-side payment options
- Data Export: Copy inputs and results to clipboard
Installation
Prerequisites
- WordPress 5.0+
- PHP 7.4+
- OpenAI API key (optional, for AI features)
Setup
-
Install the plugin:
# Copy the plugin directory to wp-content/plugins/ cp -r ai-financial-calculator /path/to/wordpress/wp-content/plugins/ -
Activate the plugin in WordPress admin
-
Configure OpenAI API key (optional):
// In wp-config.php define('OPENAI_API_KEY', 'your-api-key-here');Or set environment variable:
export OPENAI_API_KEY=your-api-key-here -
Add the shortcode to any page or post:
[ai_financial_calculator]
Configuration
Interest Rate Parameters
Filter hooks allow customization of interest rate calculations:
// Base rate (default: 7.5%)
add_filter('ai_calculator_base_rate', function() { return 8.0; });
// Lender margin (default: 1.5%)
add_filter('ai_calculator_lender_margin', function() { return 2.0; });
// Minimum profit margin (default: 5.0%)
add_filter('ai_calculator_min_profit', function() { return 6.0; });
Lender Payment Factors
Customize alternative lender payment calculations:
// Lender 1 payment factors
add_filter('ai_lender1_payment_factors', function() {
return [
12 => 0.08333,
24 => 0.05394,
36 => 0.03913,
48 => 0.03183
];
});
// Lender 2 base payments (for $10k loan)
add_filter('ai_lender2_base_payments', function() {
return [
12 => 850.00,
24 => 569.21,
30 => 432.24,
36 => 427.97,
42 => 390.41
];
});
Qualification Criteria
// Minimum credit score (default: 650)
add_filter('ai_calculator_min_credit_score', function() { return 700; });
// Allowed loan terms (default: [2, 3])
add_filter('ai_calculator_allowed_terms', function() { return [2, 3, 4]; });
Usage
Basic Calculator
- Fill in business information
- Enter loan amount and term
- Set credit score and other criteria
- Add product information for revenue calculations
- Click "Calculate & Get Results"
AI Assistant
After calculating results:
- Scroll to the AI chat section
- Ask questions about your results
- Get personalized recommendations
- Explore different scenarios
Example queries:
- "What's the best option for my situation?"
- "How can I pay off the loan faster?"
- "What if I increase my down payment to 50%?"
- "Analyze my profit margins"
Technical Details
REST API Endpoint
POST /wp-json/ai-financial-calculator/v1/chat-query
Content-Type: application/json
{
"prompt": "User's question",
"form_context": "JSON string of form data and results"
}
Data Flow
- Form Submission: POST to same page
- Processing:
form-processor.phpcalculates all values - Display: Results rendered in template
- AI Query: JavaScript sends form context + user prompt
- Response: OpenAI returns formatted markdown
- Rendering: Marked.js renders markdown to HTML
Security
- All inputs sanitized with WordPress functions (
sanitize_text_field(),wp_kses_post()) - API key stored in environment/config (never in code)
- REST API permission callback with optional nonce verification
- Rate limiting: Per-IP request throttling (configurable via
ai_calculator_rate_limitfilter) - SQL injection prevented (no direct DB queries)
- XSS protection via
esc_html(),esc_attr(),esc_url() - Prompt injection mitigation via input sanitization and system prompt isolation
Performance Considerations
- Form State: localStorage reduces server load
- API Calls: Only on user request (not automatic)
- Caching: Results can be cached per session
- Markdown: Client-side rendering (marked.js CDN)
When NOT to Use This
This plugin is not suitable for:
- ❌ Regulated financial services without compliance review and security audit
- ❌ High-traffic production sites without load testing and caching strategy
- ❌ WordPress multisite without namespace isolation testing
- ❌ Environments requiring PCI-DSS compliance without additional hardening
This is a portfolio project demonstrating technical capabilities. For production financial services, additional security hardening, compliance review, and legal consultation are required.
Portfolio Highlights
| Skill Demonstrated | Evidence |
|---|---|
| WordPress Plugin Development | REST API, shortcodes, hooks, proper architecture |
| AI API Integration | OpenAI integration with context injection |
| Financial Domain Knowledge | Amortization, interest calculations, ROI modeling |
| Full-Stack Development | PHP backend + JavaScript frontend + AI middleware |
| User Experience Design | Form persistence, real-time feedback, conversational UI |
| Security Best Practices | Input sanitization, API key management, XSS prevention |
Dependencies
- WordPress: Core functionality
- OpenAI API: For AI assistant (optional)
- Marked.js: Markdown rendering (loaded via CDN)
License
MIT License - See LICENSE file for details.
Disclaimer
This software is for demonstration purposes only. Financial calculations are simplified models and should not be used for actual lending decisions without professional financial advice. The developers are not responsible for any financial decisions made using this software.
Built to demonstrate production WordPress development with AI integration. 🚀