Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress post text corruption

Yesterday I began a small private blog using WordPress 4.1 (Windows 7, Apache 2.4, MySQL 5.5, PHP 5.6.1, UTF8, generally common settings). I started to transfer my articles into it; several of them was fine, but I found two articles which cannot be saved correctly.

When trying to save these articles as posts WordPress refreshes and shows corrupted version of text. Exact corruption is: middle of the article is removed while several garbage characters (%D? for example) inserted instead of it.

I tried to update WP to the current (4.2.2) version - error is the same (upd.: 4.2.3 - error is the same). I tried to save article as the page instead of post - error is the same. I tried the standard theme instead of custom - error is the same.

It looks like 'issued' articles are longer than others - 5.2Kb and 7.5Kb. I tried to see what happens if I would save lesser pieces. While saving a very short piece it works fine. If make a longer piece, WP losts tail of this text. If make a more longer piece, WP starts to lose the middle as described above.

Currently I have no idea how to repair or debug this case. Any suggestions?

========== Additional info ==========

I tried to execute this code into my blog:

<?php

  $my_post = array(
     'post_title' => 'Caption',
     'post_status' => 'publish',
     'post_content' => 'the very-very-long text of my article'
  );

  echo wp_insert_post( $my_post );

?>

When executed, it makes excellent post as it should be (thanks to urka_mazurka for advicing this).

When I tried to edit this post into WP, it becames corrupted when saving too. Moreover, when I tried to edit this PHP file into WP (using editing theme facility), it becomes corrupted when saving too.

===== Additional info 2 =====

Table wp_posts collation is utf8mb4_unicode_ci (result of SHOW TABLE STATUS)

It looks like urka_mazurka were the closest... at least I can publish these posts via wp_insert_post. Too bad I can't address bounty to him because he didn't publish any answer.

like image 717
Sanders the Softwarer Avatar asked Jul 21 '15 19:07

Sanders the Softwarer


1 Answers

according to your explanation the problem seems to be with the WordPress core, I'll recommend you to try a different approach:

  1. Insert a post using wp_insert_post function but without content or a "safe" content like one simple word.
  2. Then use $wpdb to update the post with the real content.

Here is a snipped of how I'll do it:

  $my_post = array(
     'post_title' => 'Caption',
     'post_status' => 'publish',
     'post_content' => 'dummy text'
  );
//get the post id
  $post_id = wp_insert_post( $my_post );

global $wpdb;
//now update the post with the real text
$wpdb->update( 
     $wpdb->prefix . 'posts', 
    array( 
        'post_content' => 'YOUR LONG TEXT HERE',    
    ), 
    array( 'ID' => $post_id ), 
    array('%s'), 
    array('%d') 
);

If you don't get the result you want with this, then is a problem with your php config or MySQL configuration unless you are willing to try a more unfriendly solution and use $wpdb->query instead of $wpdb->update like this:

$big_text = "YOUR BIG TEXT HERE";
$wpdb->query(
    "UPDATE $wpdb->posts 
    SET post_content = $big_text
    WHERE ID = $post_id"
);

Let us know.

like image 116
mibrito Avatar answered Nov 13 '22 23:11

mibrito