5

I'm working on writing a script to import data from a custom CMS into Wordpress. As part of this, I need to import the post content exactly, without modifying anything. However, some of the posts contain HTML comments which contain tags, and this seems to be confusing Wordpress.

I've distilled my script down into a small representative example:

<?php

$_SERVER['HTTP_HOST'] = 'localhost';
require_once('/var/www/wordpress/wp-load.php');

$post = array(
  'post_content' => 'This is <!-- a comment -->',
  'post_status' => 'publish'
);

wp_insert_post($post);

This works fine, and I get exactly what you would expect. If I view the source of the post in my browser, I get:

<div class="entry-content">
  <p>This is <!-- a comment --></p>
</div><!-- .entry-content -->

But if my comment contains tags in it (for example, replace the post_content with This is <!-- <p>a comment</p> -->), Wordpress escapes the comment delimiters and parses the tags inside. The comment becomes visible, and when I view source, it looks like this:

<div class="entry-content">
  <p>This is &lt;!&#8211;
    <p>a comment</p>
  <p> &#8211;&gt;</p>
</div><!-- .entry-content -->

How can I prevent Wordpress from breaking the comment delimiters and parsing the tags inside? For the record, I'm on Wordpress 3.3.1 in Ubuntu 12.04.

EDIT: Just tried it on Wordpress 3.4; the problem still occurs.

5
  • Have a look on Trac to see if this is a known issue: core.trac.wordpress.org Jun 11, 2012 at 16:03
  • Thanks for the suggestion; unfortunately, I couldn't find anything there. Jun 11, 2012 at 18:14
  • Does it happen in 3.3.2 too?
    – fuxia
    Jun 12, 2012 at 8:12
  • Just tried it on 3.3.2; it still happens. Jun 12, 2012 at 17:11
  • There's several "autop" bugs relating to HTML comments listed in the core trac, however the basic problem is that HTML comments are edge-case. Most people don't use them, and so nobody has found the time to bother to fix the issues surrounding them.
    – Otto
    Sep 15, 2012 at 14:25

1 Answer 1

2

The following program:

<?php

$_SERVER['HTTP_HOST'] = 'localhost';
require_once('wp-load.php');

$post = array(
  'post_title' => 'HTML Escape',
  'post_content' => 'This is <!-- a comment --><br/>This is <!-- <p>a comment</p> -->',
  'post_status' => 'publish'
);

$id = wp_insert_post($post);
$post = get_post($id);
var_export( esc_html( $post->post_content ) );
?>

Outputs the following in the browser:

'This is <!-- a comment --><br/>This is <!-- <p>a comment</p> -->'

So it looks like your loop might be the one that is changing the post content.

1
  • Hmm, this is very strange. The console output does indeed look correct, but when I look at the post_content field directly in the database, it shows This is <!-- a comment --><br />This is &lt;!-- <p>a comment</p> --&gt; - the two comments are escaped differently. When I view it on my blog, I'm still getting the same problem. Jun 13, 2012 at 13:08

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.