I'm building a custom Wordpress theme and I've just come across a bizarre issue where the_content() seems to be printing out the page content wrapped in the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body> ... </body></html>

This is an issue because I've now got second instances of DOCTYPE, html and body tags appearing in my page code. I've attached screenshots below showing the code and the output.

I would appreciate any help as to why this may be happening and how I can go about solving the problem. I've disabled all plugins so that doesn't seem to be the problem.


enter image description here

Edit: The content was copy and pasted directly from another webpage and it has been suggested that this was the source of the DOCTYPE and tags. I have removed all this content and manually inputted some text and the tags still exist as shown in the screenshot below.

enter image description here

  • 1
    Does that happen with a default Twenty-X theme?
    – fuxia
    Sep 4, 2019 at 23:28
  • It's a custom theme I'm developing at the moment
    – al.tron
    Sep 4, 2019 at 23:30
  • 1
    Are you sure the doctype and HTML tags are not just inside the content of that post? Sep 5, 2019 at 0:41
  • Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. Sep 5, 2019 at 1:52
  • 1
    following up on Jacob's comment: did you type the content into the editor or did you use copy/paste from a web source?
    – Michael
    Sep 5, 2019 at 4:25

1 Answer 1


Ok so, this solution is very particular to my case but maybe it can help point somebody else in the right direction in the future if they encounter a similar problem.

I have a custom function that adds a CSS class for lazy-loading images into all <img> tags that are present in the_content().

In this function I am using the following code:

$document = new DOMDocument();
$document->loadHTML( utf8_decode( $content ) );

The DOCTYPE, html tags and body tags were appearing because of PHP's loadHTML method.

Changing the above code to the following has solved this issue.

$document = new DOMDocument();
$document->loadHTML( utf8_decode( $content ), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );

I found the details I needed to solve this problem here: https://stackoverflow.com/questions/4879946/how-to-savehtml-of-domdocument-without-html-wrapper

  • I was using loadHTML for a different reason, but this solved my problem. Thanks!
    – gerard
    Sep 12, 2023 at 14:21

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.