wp update post – wp_update_post changing into unicode


I’ve run into an issue I haven’t faced before so I’m hoping someone may have an answer or solution.

I’m an updating multiple posts at the same time with wp_update_post with a string value of This is a piece of <em>string</em> as an example. The code is as follows:

foreach ( $aposts as $spost ) {
    $post_id = $spost->ID;
    $content = $spost->post_content;
    $newcontent = preg_replace( $regex, $replace_string, $content );
    $revcontent = array( 'ID' => $post_id, 'post_content' =>$newcontent, );

    $ppost = wp_update_post( $revcontent );

It loops through the posts and changes the content. However if the content is wrapped with <em></em>, it’s replaced with unicode characters e.g. u003c\u003eThis is a piece of stringu003c\u003eu003e.

If I update the content within WordPress admin by clicking on “Update”, the content changes as intended with <em></em> intact. Unfiltered HTML is enabled.

Ryan 2 years 2022-06-08T01:25:13-05:00 0 Answers 0 views 0

Leave an answer