init_knowledge_base();
}
public function init() {
// Create database table for storing conversations (optional)
$this->create_conversations_table();
}
public function init_knowledge_base() {
$this->knowledge_base = array(
// Pre-approval and Financing
‘preapproval’ => array(
‘keywords’ => array(‘preapproved’, ‘pre-approved’, ‘financing’, ‘mortgage’, ‘loan’, ‘qualify’, ‘credit’),
‘response’ => “Getting pre-approved is your first step! You’ll need to provide income verification, credit history, and debt information to a lender. This shows sellers you’re serious and gives you a clear budget. Most lenders can pre-approve you within 24-48 hours.”
),
‘down_payment’ => array(
‘keywords’ => array(‘down payment’, ‘downpayment’, ‘money down’, ‘initial payment’, ‘deposit’),
‘response’ => “Down payments typically range from 3-20% of the home’s purchase price. FHA loans allow as little as 3.5% down, while conventional loans often require 5-10%. VA loans for veterans may require 0% down. Remember to budget for closing costs too!”
),
‘credit_score’ => array(
‘keywords’ => array(‘credit score’, ‘credit rating’, ‘fico’, ‘credit history’),
‘response’ => “Most lenders prefer a credit score of 620 or higher for conventional loans. FHA loans may accept scores as low as 580. Higher scores get better interest rates. Check your credit report for errors and pay down debts to improve your score before applying.”
),
// Home Search Process
‘realtor’ => array(
‘keywords’ => array(‘realtor’, ‘agent’, ‘real estate agent’, ‘buyer agent’),
‘response’ => “A buyer’s agent represents your interests and is typically free to you (seller pays commission). Look for agents experienced in your price range and area. Interview 2-3 agents, check references, and choose someone who communicates well and understands your needs.”
),
‘home_search’ => array(
‘keywords’ => array(‘find homes’, ‘search homes’, ‘looking for house’, ‘house hunting’),
‘response’ => “Start by listing your must-haves vs. nice-to-haves. Consider location, schools, commute, and future resale value. Use online tools like MLS searches, but also drive through neighborhoods at different times. Your agent can set up automatic alerts for new listings.”
),
‘offer’ => array(
‘keywords’ => array(‘make offer’, ‘offer price’, ‘bidding’, ‘negotiate’),
‘response’ => “Your offer should be based on comparable sales, market conditions, and the home’s condition. Include contingencies for inspection, appraisal, and financing. In competitive markets, you may need to offer above asking price or waive some contingencies.”
),
// Inspection and Appraisal
‘inspection’ => array(
‘keywords’ => array(‘home inspection’, ‘inspector’, ‘inspection report’),
‘response’ => “Always get a professional inspection unless you waive this contingency to compete. Inspections cost $300-500 but can save thousands. The inspector checks structural, electrical, plumbing, and HVAC systems. You can negotiate repairs or credits based on findings.”
),
‘appraisal’ => array(
‘keywords’ => array(‘appraisal’, ‘appraiser’, ‘home value’),
‘response’ => “Your lender requires an appraisal to ensure the home’s value supports the loan amount. If it appraises below your offer price, you may need to renegotiate, pay the difference, or walk away. Appraisals typically cost $400-600.”
),
// Closing Process
‘closing_costs’ => array(
‘keywords’ => array(‘closing costs’, ‘closing fees’, ‘settlement costs’),
‘response’ => “Closing costs typically run 2-5% of the purchase price. They include loan origination fees, title insurance, attorney fees, recording fees, and prepaid taxes/insurance. Get a Loan Estimate within 3 days of applying and a Closing Disclosure 3 days before closing.”
),
‘title_insurance’ => array(
‘keywords’ => array(‘title insurance’, ‘title search’, ‘title company’),
‘response’ => “Title insurance protects you from ownership disputes or liens. The title company searches public records to ensure clear ownership. You’ll pay for lender’s title insurance (required) and can buy owner’s title insurance (recommended) for additional protection.”
),
‘final_walkthrough’ => array(
‘keywords’ => array(‘final walkthrough’, ‘walk through’, ‘final inspection’),
‘response’ => “The final walkthrough happens 24-48 hours before closing. Verify agreed-upon repairs were completed, all systems work, and the property is in the same condition as when you made your offer. This is not a second inspection – it’s a final check.”
),
// First-Time Buyer Programs
‘first_time_buyer’ => array(
‘keywords’ => array(‘first time buyer’, ‘first-time’, ‘new buyer’, ‘first home’),
‘response’ => “First-time buyers have access to special programs like FHA loans, VA loans (for veterans), USDA loans (rural areas), and state/local down payment assistance programs. Many offer lower down payments, reduced fees, or tax credits.”
),
// Market and Timing
‘market_conditions’ => array(
‘keywords’ => array(‘market conditions’, ‘buyer market’, ‘seller market’, ‘housing market’),
‘response’ => “In a buyer’s market, you have more negotiating power and inventory to choose from. In a seller’s market, you’ll face competition and may need to act quickly. Your agent can help you understand current local market conditions and adjust your strategy accordingly.”
),
‘best_time’ => array(
‘keywords’ => array(‘best time to buy’, ‘when to buy’, ‘timing’),
‘response’ => “The best time to buy is when you’re financially ready and plan to stay put for at least 5-7 years. Spring and summer are popular but competitive. Fall and winter may offer less competition but fewer listings. Focus on your personal readiness over market timing.”
),
// Default responses
‘greeting’ => array(
‘keywords’ => array(‘hello’, ‘hi’, ‘hey’, ‘good morning’, ‘good afternoon’),
‘response’ => “Hello! I’m here to help answer your home buying questions. I can provide information about pre-approval, down payments, the search process, inspections, closing costs, and much more. What would you like to know?”
),
‘thanks’ => array(
‘keywords’ => array(‘thank you’, ‘thanks’, ‘appreciate’),
‘response’ => “You’re welcome! I’m here to help make your home buying journey smoother. Do you have any other questions about the home buying process?”
)
);
}
public function enqueue_scripts() {
wp_enqueue_script(‘jquery’);
wp_enqueue_script(‘chatbot-js’, plugin_dir_url(__FILE__) . ‘chatbot.js’, array(‘jquery’), ‘1.0’, true);
wp_enqueue_style(‘chatbot-css’, plugin_dir_url(__FILE__) . ‘chatbot.css’, array(), ‘1.0’);
// Localize script for AJAX
wp_localize_script(‘chatbot-js’, ‘chatbot_ajax’, array(
‘ajax_url’ => admin_url(‘admin-ajax.php’),
‘nonce’ => wp_create_nonce(‘chatbot_nonce’)
));
}
public function render_chatbot() {
?>
🏠 Home Buying Assistant
💰 Down Payment
📋 Pre-Approval
🏠 Closing Costs
🔍 Inspection
💬
find_best_response($user_input);
// Optional: Log conversation
$this->log_conversation($user_input, $response);
wp_send_json_success(array(
‘response’ => $response,
‘timestamp’ => current_time(‘g:i A’)
));
}
private function find_best_response($input) {
$input_lower = strtolower($input);
$best_match = null;
$best_score = 0;
foreach ($this->knowledge_base as $topic => $data) {
$score = 0;
foreach ($data[‘keywords’] as $keyword) {
if (strpos($input_lower, strtolower($keyword)) !== false) {
$score += strlen($keyword);
}
}
if ($score > $best_score) {
$best_score = $score;
$best_match = $data[‘response’];
}
}
if ($best_match) {
return $best_match;
}
// Default response if no match
return “I’d be happy to help with your home buying question! I can provide information about pre-approval, down payments, finding homes, inspections, closing costs, and more. Could you be more specific about what you’d like to know?”;
}
private function create_conversations_table() {
global $wpdb;
$table_name = $wpdb->prefix . ‘chatbot_conversations’;
$charset_collate = $wpdb->get_charset_collate();
$sql = “CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
user_message text NOT NULL,
bot_response text NOT NULL,
user_ip varchar(45) DEFAULT ”,
timestamp datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) $charset_collate;”;
require_once(ABSPATH . ‘wp-admin/includes/upgrade.php’);
dbDelta($sql);
}
private function log_conversation($user_message, $bot_response) {
global $wpdb;
$table_name = $wpdb->prefix . ‘chatbot_conversations’;
$wpdb->insert(
$table_name,
array(
‘user_message’ => $user_message,
‘bot_response’ => $bot_response,
‘user_ip’ => $_SERVER[‘REMOTE_ADDR’]
)
);
}
public function add_admin_menu() {
add_options_page(
‘Chatbot Settings’,
‘Home Buyer Chatbot’,
‘manage_options’,
‘home-buyer-chatbot’,
array($this, ‘admin_page’)
);
}
public function admin_page() {
global $wpdb;
$table_name = $wpdb->prefix . ‘chatbot_conversations’;
// Get recent conversations
$conversations = $wpdb->get_results(
“SELECT * FROM $table_name ORDER BY timestamp DESC LIMIT 50”
);
?>
Need Help?
Home Buyer Chatbot
Recent Conversations
Date | User Question | Bot Response | IP Address |
---|---|---|---|
timestamp)); ?> | user_message, 0, 100)); ?>user_message) > 100 ? ‘…’ : ”; ?> | bot_response, 0, 100)); ?>bot_response) > 100 ? ‘…’ : ”; ?> | user_ip); ?> |
Knowledge Base Topics
- Pre-approval & Financing: Mortgage pre-approval, credit scores, down payments
- Home Search: Finding agents, searching for homes, making offers
- Inspections & Appraisals: Home inspections, appraisal process
- Closing Process: Closing costs, title insurance, final walkthrough
- First-Time Buyer Programs: FHA, VA, USDA loans, assistance programs
- Market Conditions: Timing, market trends, buyer vs seller markets