0

I have problem with encoding on saving custom post type in Wordpress. When I hit update, the UTF-8 characters (č, š, ž, ø, etc.) are tranformed to "u010d" and others.

The problem seems to be with form. I recieve already broken characters through POST.

I have saved file with UTF-8 encoding and I have meta tag for encoding in HEAD of HTML.

What can I do to fix this?

Thank you!

Edit:

I have accept-charset="UTF-8" in form.

HEAD: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

Function:

add_action( 'save_post', 'layered_images_save_info' );

function layered_images_save_info( $post_id ) {

// verify nonce

if ( ! wp_verify_nonce( $_POST['layered_images_box_nonce'], basename( __FILE__ ) ) ) {

   return $post_id;

}

// check autosave

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {

   return $post_id;

}
// check permissions

if ( 'layered_images' == $_POST[ 'post_type'] && current_user_can( 'edit_post', $post_id ) ) {

   /* Save Slider Images */

   //echo "";print_r($_POST['gallery_img']);exit;
    //$bla  = html_entity_decode($_POST[ 'layer_titles' ], ENT_QUOTES, "utf-8");
    $gallery_images = ( isset( $_POST[ 'gallery_img' ] ) ? $_POST[ 'gallery_img' ] : '' );
    $layer_opacity = ( isset( $_POST[ 'layer_opacity' ] ) ? $_POST[ 'layer_opacity' ] : '' );
    $layer_color = ( isset( $_POST[ 'layer_color' ] ) ? $_POST[ 'layer_color' ] : '' );
   //print_r($bla);exit;
    $gallery_images = strip_tags( json_encode( $gallery_images ) );

    $visible_layers = ( isset( $_POST[ 'visible_layers' ] ) ? $_POST[ 'visible_layers' ] : '' );
    $visible_layers = strip_tags( json_encode( $visible_layers ) );

    $visible_user_layers = ( isset( $_POST[ 'visible_user_layers' ] ) ? $_POST[ 'visible_user_layers' ] : '' );
    $visible_user_layers = strip_tags( json_encode( $visible_user_layers ) );

    $layer_titles = ( isset( $_POST[ 'layer_titles' ] ) ? $_POST[ 'layer_titles' ] : '' );
    $layer_titles =  json_encode( $layer_titles ) ;

    update_post_meta( $post_id, "_layer_gallery_images", $gallery_images );
    update_post_meta( $post_id, "_layer_visible_layers", $visible_layers );
    update_post_meta( $post_id, "_visible_user_layers", $visible_user_layers );
    update_post_meta( $post_id, "_layer_titles", $layer_titles );
    update_post_meta( $post_id, "_layer_opacity", $layer_opacity );
    update_post_meta( $post_id, "_layer_color", $layer_color );


} else {

   return $post_id;

}

}

Jure Jager
  • 146
  • 1
  • 3
  • 11
  • You could try specifing the charset for the form: `
    `. If that doesn't change anything, please post your html header where you declare encoding and the function where you process received form data.
    – MSTannu Sep 10 '14 at 19:38
  • UPDATE: Problem only occurs on fields which are listed with php loop, with data from postmeta field. And on fields which are dynamically added with jQuery. This fields have name like this: `layer_titles[]`. @MSTannu I have updated my question. Please check. – Jure Jager Sep 11 '14 at 05:36

1 Answers1

0

It appears saving unicode characters like that is a feature of json_encode. You could either force it not to do that, with

json_encode( $text, JSON_UNESCAPED_UNICODE );

as seen in this topic: Why does the PHP json_encode function convert UTF-8 strings to hexadecimal entities?

OR don't use json_encode at all. WP has its own serializing method built in for saving arrays in options and meta fields.

Community
  • 1
  • 1
MSTannu
  • 1,033
  • 1
  • 6
  • 14