Frage

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.

Code

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

War es hilfreich?

Lösung

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit wordpress.stackexchange
scroll top