Text cut off on save

Your browser doesn’t send the copied text UTF-8 encoded to the server. On the first character that isn’t UTF-8 compatible, in your example the apostrophe – ’ –, the stream to the data base is broken and not recovered. That’s a browser issue.
If I had to guess – well, I have because you didn’t mention it – I would say Internet Explorer.

There are two ways to fix that.

  1. Get a better browser.

  2. Make sure everything you try to save is UTF-8 compatible. And here is finally a chance to bring this issue on topic for our site. 🙂

You can filter 'wp_insert_post_data' and prepare all the data. The following code should work as a plugin (not tested, it is just a prototype).

<?php
/* Plugin Name: Force UTF-8 */
add_filter( 'wp_insert_post_data', 'wpse_56411_force_utf8' );

function wpse_56411_force_utf8( $data ) 
{
    $out = array();
    foreach ( $data as $key => $value )
    {
        if ( ! seems_utf8( $value ) )
        {
            if ( function_exists( 'mb_convert_encoding' ) )
            {
                $out[ $key ] = mb_convert_encoding( $value, 'UTF-8', 'windows-1252' );
            }
            elseif ( function_exists( 'iconv' ) )
            {
                $out[ $key ] = iconv( 'windows-1252', 'UTF-8', $value );
            }
            else
            {
                $out[ $key ] = utf8_encode( $value );
            }
        }
        else
        {
            $out[ $key ] = $value;
        }
    }
    return $out;
}

Leave a Comment