最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - the_content() printing DOCTYPE, <html> and <body> tags in the middle of page

programmeradmin0浏览0评论

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" ".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.

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.

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/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.

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.

Share Improve this question edited Sep 5, 2019 at 14:25 al.tron asked Sep 4, 2019 at 23:23 al.tronal.tron 1396 bronze badges 7
  • 1 Does that happen with a default Twenty-X theme? – fuxia Commented Sep 4, 2019 at 23:28
  • It's a custom theme I'm developing at the moment – al.tron Commented Sep 4, 2019 at 23:30
  • 1 Are you sure the doctype and HTML tags are not just inside the content of that post? – Jacob Peattie Commented 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. – Matthew Brown aka Lord Matt Commented 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 Commented Sep 5, 2019 at 4:25
 |  Show 2 more comments

1 Answer 1

Reset to default 2

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/questions/4879946/how-to-savehtml-of-domdocument-without-html-wrapper

发布评论

评论列表(0)

  1. 暂无评论