元盒不会在wordpress中保存html数据
我使用这里给出的脚本 它工作正常,但问题是它不保存html代码/标记。当我编写测试时可以,但当我将任何html代码放入普通文本区域或自定义wp_编辑器时,它不保存数据 下面是保存数据的函数的一部分,您知道如何让它工作以保存html源代码吗? 谢谢元盒不会在wordpress中保存html数据,wordpress,meta-boxes,Wordpress,Meta Boxes,我使用这里给出的脚本 它工作正常,但问题是它不保存html代码/标记。当我编写测试时可以,但当我将任何html代码放入普通文本区域或自定义wp_编辑器时,它不保存数据 下面是保存数据的函数的一部分,您知道如何让它工作以保存html源代码吗? 谢谢 这里的问题是,metaboxes脚本使用的默认清理函数正在从输入中剥离html内容 下面的代码将通过说明您想要使用不同的消毒剂来解决这个问题 function save_box( $post_id ) { $post_type = get_po
这里的问题是,metaboxes脚本使用的默认清理函数正在从输入中剥离html内容 下面的代码将通过说明您想要使用不同的消毒剂来解决这个问题
function save_box( $post_id ) {
$post_type = get_post_type();
// verify nonce
if ( ! isset( $_POST['custom_meta_box_nonce_field'] ) )
return $post_id;
if ( ! ( in_array( $post_type, $this->page ) || wp_verify_nonce( $_POST['custom_meta_box_nonce_field'], 'custom_meta_box_nonce_action' ) ) )
return $post_id;
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// check permissions
if ( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
// loop through fields and save the data
foreach ( $this->fields as $field ) {
if( $field['type'] == 'section' ) {
$sanitizer = null;
continue;
}
if( in_array( $field['type'], array( 'tax_select', 'tax_checkboxes' ) ) ) {
// save taxonomies
if ( isset( $_POST[$field['id']] ) ) {
$term = $_POST[$field['id']];
wp_set_object_terms( $post_id, $term, $field['id'] );
}
}
else {
// save the rest
$new = false;
$old = get_post_meta( $post_id, $field['id'], true );
if ( isset( $_POST[$field['id']] ) )
$new = $_POST[$field['id']];
if ( isset( $new ) && '' == $new && $old ) {
delete_post_meta( $post_id, $field['id'], $old );
} elseif ( isset( $new ) && $new != $old ) {
// the code below is commented out and replaced with a line that specifically sets the sanitizer to one that will keep some html
//$sanitizer = isset( $field['sanitizer'] ) ? $field['sanitizer'] : 'sanitize_text_field';
$sanitizer = 'wp_kses_post'
if ( is_array( $new ) )
$new = meta_box_array_map_r( 'meta_box_sanitize', $new, $sanitizer );
else
$new = meta_box_sanitize( $new, $sanitizer );
update_post_meta( $post_id, $field['id'], $new );
}
}
} // end foreach
}
请注意,虽然这适用于您的情况,但也意味着当有更好的替代方案可用于其他数据类型时,所有其他元盒都将使用相同的清理程序。这里的问题是,元盒脚本使用的默认清理功能正在从您的数据中剥离html内容输入 下面的代码将通过说明您想要使用不同的消毒剂来解决这个问题
function save_box( $post_id ) {
$post_type = get_post_type();
// verify nonce
if ( ! isset( $_POST['custom_meta_box_nonce_field'] ) )
return $post_id;
if ( ! ( in_array( $post_type, $this->page ) || wp_verify_nonce( $_POST['custom_meta_box_nonce_field'], 'custom_meta_box_nonce_action' ) ) )
return $post_id;
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// check permissions
if ( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
// loop through fields and save the data
foreach ( $this->fields as $field ) {
if( $field['type'] == 'section' ) {
$sanitizer = null;
continue;
}
if( in_array( $field['type'], array( 'tax_select', 'tax_checkboxes' ) ) ) {
// save taxonomies
if ( isset( $_POST[$field['id']] ) ) {
$term = $_POST[$field['id']];
wp_set_object_terms( $post_id, $term, $field['id'] );
}
}
else {
// save the rest
$new = false;
$old = get_post_meta( $post_id, $field['id'], true );
if ( isset( $_POST[$field['id']] ) )
$new = $_POST[$field['id']];
if ( isset( $new ) && '' == $new && $old ) {
delete_post_meta( $post_id, $field['id'], $old );
} elseif ( isset( $new ) && $new != $old ) {
// the code below is commented out and replaced with a line that specifically sets the sanitizer to one that will keep some html
//$sanitizer = isset( $field['sanitizer'] ) ? $field['sanitizer'] : 'sanitize_text_field';
$sanitizer = 'wp_kses_post'
if ( is_array( $new ) )
$new = meta_box_array_map_r( 'meta_box_sanitize', $new, $sanitizer );
else
$new = meta_box_sanitize( $new, $sanitizer );
update_post_meta( $post_id, $field['id'], $new );
}
}
} // end foreach
}
请注意,虽然这适用于您的情况,但也意味着当有更好的替代方案可用于其他数据类型时,所有其他元盒都将使用相同的净化剂。通常净化剂会从文本输入中删除html。看看这个函数:
meta\u-box\u-sanitize()
,看看它是如何净化文本的。它可能是剥离html内容的地方。查看metaboxes脚本的清理功能,我发现大多数清理程序都会剥离html内容。更新时设置了哪个$sanitizer
?谢谢,是的,我正在检查并尝试禁用,但尚未找到解决方案。我想永久禁用sanitier他们还增加了使用visual editor的可能性,但是它在保存帖子后不起作用,它会删除代码末尾附近的所有html数据。请尝试将sanitizer设置为更适合html的值,如$sanitizer='wp\u kses\u post'
。这将允许一些html通过。再次使用类似于…
标记或其中的链接来测试保存metabox,并查看其是否存储。通常情况下,消毒剂会从文本输入中删除html。看看这个函数:meta\u-box\u-sanitize()
,看看它是如何净化文本的。它可能是剥离html内容的地方。查看metaboxes脚本的清理功能,我发现大多数清理程序都会剥离html内容。更新时设置了哪个$sanitizer
?谢谢,是的,我正在检查并尝试禁用,但尚未找到解决方案。我想永久禁用sanitier他们还增加了使用visual editor的可能性,但是它在保存帖子后不起作用,它会删除代码末尾附近的所有html数据。请尝试将sanitizer设置为更适合html的值,如$sanitizer='wp\u kses\u post'
。这将允许一些html通过。使用类似于…
标记或链接的东西再次测试保存代谢箱,并查看它是否存储。这是一个出色的解决方案。谢谢。这是一个绝妙的解决方案。非常感谢。