Case Study: Fixing Mixed Content Issues in WordPress with a Simple PHP Snippet
The Problem
Today I was working on Technical SEO fixes on WordPress website, and Semrush flagged a critical issue: 6,125 links on HTTPS pages lead to HTTP pages.
This is a common mixed content problem. It occurs when a website has already been migrated to HTTPS but still contains old internal links or resources pointing to HTTP. These inconsistencies can cause browser warnings, reduce trust, and negatively affect SEO performance.
The Solution
To address this, I created a simple PHP snippet that automatically replaces all http:// links with https:// before the page is sent to the browser. I added this code using the Code Snippets plugin for safety and ease of management.
Code Implementation
// Force replace all http:// to https:// in site content
function nn_force_https_output($buffer) {
return str_replace("http://", "https://", $buffer);
}
function nn_start_https_replace() {
ob_start("nn_force_https_output");
}
function nn_end_https_replace() {
if (ob_get_length()) {
ob_end_flush();
}
}
// Start output buffering
add_action('template_redirect', 'nn_start_https_replace');
add_action('shutdown', 'nn_end_https_replace');
The Results
- The SEMrush report was cleared, with all errors dropping to zero.
- Every HTTP link on HTTPS pages is now automatically converted to HTTPS.
- Browser mixed content warnings were fully resolved.
This small technical adjustment had an immediate impact, saving hours of manual corrections and improving the site’s SEO health.