2016-03-04 78 views
0

爲什麼wp_editor刪除我所有的html標籤?WordPress wp_editor刪除html標籤 - 如何阻止它?

這是我的代碼:

/** 
* Outputs the content of the meta box. 
*/ 
function prfx_meta_callback($post) { 
    // echo 'This is a meta box'; 
    wp_nonce_field(basename(__FILE__), 'prfx_nonce'); 
    $prfx_stored_meta = get_post_meta($post->ID); 

    $field_value = get_post_meta($post->ID, 'meta-textarea', false); 

    // Settings that we'll pass to wp_editor 
    $args = array (
     'textarea_rows' => 4, 
     'teeny'   => true, 
     // 'media_buttons' => false, 
    ); 
    ?> 

    <p> 
     <label for="meta-text" class="prfx-row-title"><?php _e('Example Text Input', 'prfx-textdomain')?></label> 
     <input type="text" name="meta-text" id="meta-text" value="<?php if (isset ($prfx_stored_meta['meta-text'])) echo $prfx_stored_meta['meta-text'][0]; ?>" /> 
    </p> 

    <label for="meta-textarea" class="prfx-row-title"><?php _e('Example Textarea Input', 'prfx-textdomain')?></label> 
    <?php wp_editor($field_value[0], 'meta-textarea', $args);?> 

    <?php 
} 

/** 
* Saves the custom meta input. 
*/ 
function prfx_meta_save($post_id) { 
    // Checks save status 
    $is_autosave = wp_is_post_autosave($post_id); 
    $is_revision = wp_is_post_revision($post_id); 
    $is_valid_nonce = (isset($_POST[ 'prfx_nonce' ]) && wp_verify_nonce($_POST[ 'prfx_nonce' ], basename(__FILE__))) ? 'true' : 'false'; 

    // Exits script depending on save status 
    if ($is_autosave || $is_revision || !$is_valid_nonce) { 
     return; 
    } 

    // Checks for input and sanitizes/saves if needed 
    if(isset($_POST[ 'meta-text' ])) { 
     update_post_meta($post_id, 'meta-text', sanitize_text_field($_POST[ 'meta-text' ])); 
    } 

    // Checks for input and sanitizes/saves if needed 
    if(isset($_POST[ 'meta-textarea' ])) { 
     update_post_meta($post_id, 'meta-textarea', sanitize_text_field($_POST[ 'meta-textarea' ])); 
    } 

} 
add_action('save_post', 'prfx_meta_save'); 

我怎樣才能阻止它從刪除HTML標籤?

比如我在此類型:

<img src="http://xxxx.jpg" alt="10168088_719806568132380_3368979362641476670_n" width="300" height="214" class="alignnone size-medium wp-image-96" /> 

它刪除。

但是,如果我以純文本格式不包含HTML標籤類型,

abcde. 

它保存它。

任何想法?

回答

4

sanitize_text_field()清理您的輸入。

所以用

update_post_meta($post_id, 'meta-textarea', stripslashes($_POST[ 'meta-textarea' ])); 

更換

update_post_meta($post_id, 'meta-textarea', sanitize_text_field($_POST[ 'meta-textarea' ])); 

可以解決你的問題。

2

從用戶輸入或從數據庫中清理字符串。

檢查無效的UTF-8,將單個<字符轉換爲實體,剝離所有標籤,刪除換行符,製表符和額外的空白區域,剝離八位字節。 嘗試從update_post_meta($post_id, 'meta-textarea', sanitize_text_field($_POST[ 'meta-textarea' ]));中刪除清潔功能