Would anyone know a function or how to remove <!-- plugin inserted stuff--> from the header and footer so that they won't be visible when viewing source?

  • I don't think this is possible - not all browsers show comments either. So I think your choices are to either remove the html comments manually or you can re-write the comments you need as PHP comments instead since those aren't visible in the source, but still visible in the files for documentation purposes.
    – RachieVee
    Jun 26, 2014 at 3:19

1 Answer 1


This is possible. I have tested it on my own server, but it might be better to just remove the comments yourself, or put them as php comments regardless.

I only demo'd this with header.php, and used the code from this page to assist me.

First of all, take the entire contents of your header.php (or whatever other file you want to remove comments from) and put them into a new file like header2.php - call it whatever you want, just remember it for later. Then, in your now empty header.php, put the following php code:

// Remove unwanted HTML comments
include 'header2.php';
$newheader = ob_get_clean();
function remove_html_comments($content) {
    echo preg_replace('/<!--(.|\s)*?-->/', '', $content);

Remember: If you named your new file something other than header2.php, change it's name accordingly on line 4 of the code!

It stores the response from your old header.php in an output buffering, then runs the nifty code that we got from the post I linked to earlier, and echoes the output.

This produced the desired affect, and the echoed code still showed stuff like what page I was on, etc - enjoy! (Ps. Use at own risk)

As suggested by another member, you could just name header2.php to something along the lines of header-custom.php which in effect lets you use get_header(custom), instead of having to use an output buffer (I think... not too sure, haven't tested). This is probably a more correct method of naming, at the very least it might help if you're making a theme to sell it.

  • Just name your new file header-custom.php and call it like get_header('custom'); Jun 26, 2014 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.