【问题标题】:Wordpress custom post type encodingWordpress 自定义帖子类型编码
【发布时间】:2014-09-10 19:23:22
【问题描述】:

我在 Wordpress 中保存自定义帖子类型时遇到编码问题。当我点击更新时,UTF-8 字符(č、š、ž、ø 等)被转换为“u010d”等。

问题似乎出在形式上。我通过 POST 收到已经损坏的字符。

我已经使用 UTF-8 编码保存了文件,并且我在 HTML 的 HEAD 中有用于编码的元标记。

我能做些什么来解决这个问题?

谢谢!

编辑:

我的表单中有accept-charset="UTF-8"

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

功能:

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;

}

}

【问题讨论】:

  • 您可以尝试为表单指定字符集:<form action="demo_form.asp" accept-charset="UTF-8">。如果这没有任何改变,请将您的 html 标头发布在您声明编码的位置以及处理接收到的表单数据的功能。
  • 更新:问题仅发生在使用 php 循环列出的字段上,数据来自 postmeta 字段。在使用 jQuery 动态添加的字段上。该字段的名称如下:layer_titles[]。 @MSTannu 我已经更新了我的问题。请检查。

标签: php wordpress encoding utf-8 custom-post-type


【解决方案1】:

看起来像这样保存 unicode 字符是 json_encode 的一个功能。您可以使用

强制它不这样做
json_encode( $text, JSON_UNESCAPED_UNICODE );

如本主题所示:Why does the PHP json_encode function convert UTF-8 strings to hexadecimal entities?

或者根本不使用 json_encode。 WP 内置了自己的序列化方法,用于将数组保存在选项和元字段中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    相关资源
    最近更新 更多