HTML标签在存储到数据库,WordPress自定义帖子类型,编辑器时被剥离


html tags are getting stripped out on storing to database, wordpress custom post type, editor

    if ( ! isset( $_POST['myplugin_new_field'] ) ) {
        return;
    }
    if ( ! isset( $_POST['myplugin_new_field2'] ) ) {
        return;
    }
    if ( ! isset( $_POST['myplugin_new_field3'] ) ) {
        return;
    }
    // Sanitize user input.
    $my_data =   sanitize_text_field($_POST['myplugin_new_field']);
    $my_data2 =  sanitize_text_field($_POST['myplugin_new_field2']);
    $my_data3 =  sanitize_text_field($_POST['myplugin_new_field3']);
    // Update the meta field in the database.
    update_post_meta( $post_id, '_my_meta_value_key', $my_data );
    update_post_meta( $post_id, '_my_meta_value_key2', $my_data2 );
    update_post_meta( $post_id, '_my_meta_value_key3', $my_data3 );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );

我使用的是默认编辑器,所有 HTML 标签都在存储时被剥离。

这是因为sanitize_text_field((根据Wordpress函数参考删除了所有标签。

检查无效的 UTF-8,将单个<字符转换为实体,去除所有标记,删除换行符、制表符和额外的空格,去除八位字节。>