【发布时间】:2016-09-12 08:43:00
【问题描述】:
我正在使用这里给出的脚本https://github.com/awshout/Custom-WordPress-Meta-Boxes/tree/master/metaboxes
它工作正常,但问题是它不保存 html 代码/标签。当我编写测试时可以,但是当我将任何 html 代码放在普通 textareas 或 c ustom wp_editor 中时,它不会保存数据。
下面是保存数据的部分功能你知道如何让它工作来保存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 ) {
$sanitizer = isset( $field['sanitizer'] ) ? $field['sanitizer'] : 'sanitize_text_field';
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_box_sanitize()看看它是如何净化文本的。它可能是剥离 html 内容的地方。 -
查看元框脚本的清理功能,我发现大多数清理程序都会删除 html 内容。更新时设置了哪个
$sanitizer? -
谢谢,是的,我正在检查并尝试禁用但找不到解决方案。我想永久禁用 sanitier
-
他们也增加了使用可视化编辑器的可能性,但保存帖子后它无法正常工作,它会删除所有 html 数据
-
在您的代码接近尾声时,尝试将 sanitizer 设置为更允许 html 的内容,例如
$sanitizer = 'wp_kses_post'。这将允许一些html通过。使用<p>....</p>标签或其中的链接再次测试保存元框,看看它是否存储。
标签: wordpress meta-boxes