<?php
function get_live_rate($url, $regex, $label) {
$context = stream_context_create([
‘http’ => [
‘user_agent’ => ‘Mozilla/5.0 (compatible; MortgageBot/1.0)’,
‘timeout’ => 5
]
]);
$html = @file_get_contents($url, false, $context);

if (!$html) {
return [$label, ‘Unavailable’, $url];
}

preg_match($regex, $html, $matches);
$rate = isset($matches[1]) ? $matches[1] . ‘%’ : ‘Unavailable’;

return [$label, $rate, $url];
}

function mortgage_rates_table_live() {
$rates = [];

// Live scraping
$rates[] = get_live_rate(
“https://www.rbcroyalbank.com/mortgages/mortgage-rates.html”,
‘/5-Year Fixed.*?(\d+\.\d+)%/’,
“RBC Royal Bank”
);

$rates[] = get_live_rate(
“https://www.td.com/ca/en/personal-banking/products/mortgages/mortgage-rates/”,
‘/5-Year Fixed.*?(\d+\.\d+)%/’,
“TD Canada Trust”
);

$rates[] = get_live_rate(
“https://www.ratehub.ca/best-mortgage-rates/five-year/fixed”,
‘/Lowest rate.*?(\d+\.\d+)%/’,
“Ratehub”
);

// Fallbacks for providers that don’t allow scraping or use JavaScript
$rates[] = [‘Scotiabank’, ‘Unavailable’, ‘https://www.scotiabank.com/ca/en/personal/mortgages/mortgage-rates.html’];
$rates[] = [‘BMO’, ‘Unavailable’, ‘https://www.bmo.com/main/personal/mortgages/rates/’];
$rates[] = [‘CIBC’, ‘Unavailable’, ‘https://www.cibc.com/en/personal-banking/mortgages/mortgage-rates.html’];
$rates[] = [‘LowestRates.ca’, ‘Unavailable’, ‘https://www.lowestrates.ca/mortgage’];
$rates[] = [‘Dominion Lending’, ‘Unavailable’, ‘https://www.dominionlending.ca/mortgage-rates/’];

// Build HTML output
ob_start(); ?>
<div style=”max-width:800px; font-family:Arial; border:1px solid #ccc; padding:20px; border-radius:10px;”>
<h2 style=”color:#1a5e2e;”>Current 5-Year Fixed Mortgage Rates in Canada</h2>
<p><strong>Date:</strong> <?php echo date(“F j, Y”); ?></p>
<table style=”width:100%; border-collapse:collapse; margin-top:10px;”>
<thead>
<tr style=”background:#f1f1f1;”>
<th style=”text-align:left; padding:8px;”>Provider</th>
<th style=”text-align:left; padding:8px;”>5-Year Fixed</th>
<th style=”text-align:left; padding:8px;”>Link</th>
</tr>
</thead>
<tbody>
<?php foreach ($rates as $row): ?>
<tr>
<td style=”padding:8px;”><?php echo htmlspecialchars($row[0]); ?></td>
<td style=”padding:8px;”><?php echo htmlspecialchars($row[1]); ?></td>
<td style=”padding:8px;”><a href=”<?php echo esc_url($row[2]); ?>” target=”_blank”>Visit</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p style=”font-size:0.9em; color:gray; margin-top:10px;”>
Data pulled live on <?php echo date(“F j, Y, g:i a”); ?>. Always confirm rates with the official provider.
</p>
</div>
<?php
return ob_get_clean();
}

add_shortcode(‘mortgage_rates’, ‘mortgage_rates_table_live’);

Scroll to Top