[wp-trac] [WordPress Trac] #47420: Block markup containing HTML in block attributes is corrupted when using wp_insert_post

WordPress Trac noreply at wordpress.org
Wed Aug 30 18:26:33 UTC 2023


#47420: Block markup containing HTML in block attributes is corrupted when using
wp_insert_post
--------------------------+------------------------------
 Reporter:  modernnerd    |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  General       |     Version:
 Severity:  normal        |  Resolution:
 Keywords:  2nd-opinion   |     Focuses:
--------------------------+------------------------------

Comment (by Jules Colle):

 I'm practically doing the same thing as @modernnerd. I'm copying code from
 the block editor to a HTML file, which I use later to dynamically insert
 pages in my testing/development environment.

 At some point, I'm creating the page from the HTML file like this:


 {{{
 $page_content = file_get_contents('/path/to/test-page.html');
 $page_id = wp_insert_post([
   //...
   'post_content' => $page_content,
 ]);
 }}}

 With this code I ran into the problem where the actual content on the page
 had `u003c/strongu003e` all over the place.

 Simply adding `addslashes($page_content)` seems to fix all these problems
 for me.

 Working code:
 {{{
 $page_content = file_get_contents('/path/to/test-page.html');
 $page_content = addslashes($page_content);
 $page_id = wp_insert_post([
   //...
   'post_content' => $page_content,
 ]);
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47420#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list