html - WordPress wp_editor 删除 html 标签 - 如何阻止它?

标签 html wordpress tinymce wp-editor

为什么 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.

它保存它。

有什么想法吗?

最佳答案

sanitize_text_field() 清理您的输入。

所以替换

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

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

可能会解决您的问题。

关于html - WordPress wp_editor 删除 html 标签 - 如何阻止它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35792810/

相关文章:

javascript - 有没有办法在重定向到空白窗口等待使用 JavaScript 加载之前加载同一域下的整个 URL?

html - 我需要两个元素通过 CSS 切换位置

php - 在 WooCommerce 中使用 PHP 按 ID 删除产品

php - 使用 Woo Cancel for Customers Plugin 在我的帐户订单列表中添加取消按钮

jquery - TinyMCE - 我们能否拥有一个工具栏面板来控制多个实例?

html - 使用 gulp-useref 构建应用程序时尊重 CSS @import

javascript - 如何使用 JavaScript 将事件监听器添加到服务器返回的不同数量的元素中

php - WordPress:找不到对象

javascript - 在 TinyMCE 中保存替换变量,但在可视化编辑器中显示替换内容

php - 停止 TinyMCE 在 WordPress 中剥离空标签 - 是的,我已经研究过了