metaboxes不保存wordpress中的html数据

时间:2016-09-12 08:43:01

标签: wordpress meta-boxes

我正在使用此处提供的脚本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
}

1 个答案:

答案 0 :(得分:1)

这里的问题是元文件脚本使用的默认清理函数正在从输入中删除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
}

请注意,虽然这适用于您的情况,但也意味着当有更好的替代品可用于其他数据类型时,所有其他元数据将通过同一个清理程序。