Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Created November 26, 2025 11:58
Show Gist options
  • Select an option

  • Save andrewlimaza/7f2b38359f7d299635bc4639f9f013f8 to your computer and use it in GitHub Desktop.

Select an option

Save andrewlimaza/7f2b38359f7d299635bc4639f9f013f8 to your computer and use it in GitHub Desktop.
Remove HTML comments from source code for WordPress sites.
<?php
/**
* Remove all comments from HTML source (<!-- some comment -->)
* Use at your own risk.
*
* To add customizations to your site, please follow this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
// Strip out HTML comments from the buffer.
function my_pmpro_strip_html_comments( $buffer ) {
$buffer = preg_replace( '/<!--(.|s)*?-->/', '', $buffer );
return $buffer;
}
// Start the buffer on <head>
function my_pmpro_buffer_start() {
ob_start( 'my_pmpro_strip_html_comments' );
}
add_action( 'wp_head', 'my_pmpro_buffer_start', 1 );
// Close the buffer when PHP stops executing.
function my_pmpro_buffer_end() {
ob_end_flush();
}
add_action( 'shutdown', 'my_pmpro_buffer_end' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment