强制纯文本粘贴

 敖军士 发布于 2022-12-31 10:00

WP 3.9.1 TinyMCE 4.x

我在我网站的前端使用wp_editor().问题是当用户粘贴一些带有样式(粗体,颜色......)的内容时,它会出现在tinyMCE4编辑器中.

如何防止粘贴样式?我只想要粘贴文本.

这是代码:

首先,tiny_mce_before_init过滤器:

function mytheme_job_tinymce_settings( $in ) {

    $in['remove_linebreaks'] = true;
    $in['gecko_spellcheck'] = false;
    $in['keep_styles'] = false;
    $in['accessibility_focus'] = true;
    $in['tabfocus_elements'] = 'major-publishing-actions';
    $in['media_strict'] = false;
    $in['paste_remove_styles'] = true;
    $in['paste_remove_spans'] = true;
    $in['paste_strip_class_attributes'] = 'all';
    $in['paste_text_use_dialog'] = false;
    $in['wpeditimage_disable_captions'] = true;
    $in['plugins'] = 'tabfocus,paste';
    $in['wpautop'] = false;
    $in['apply_source_formatting'] = false;
    $in['toolbar1'] = 'bold,italic,underline,strikethrough,bullist,numlist,hr,alignleft,aligncenter,alignright,undo,redo ';
    $in['toolbar2'] = '';
    $in['toolbar3'] = '';
    $in['toolbar4'] = '';

    return $in;

}

add_filter( 'tiny_mce_before_init', 'mytheme_job_tinymce_settings' );

然后在前端(页面模板)中使用wp_editor():

wp_editor( stripslashes( $profile ) , 'profile', array(
    'textarea_name'    => 'profile',
    'textarea_rows'    => 12,
    'media_buttons'    => false,
    'teeny'            => false,
    'editor_css'       => '',
    'tinymce'          => array(
        'content_css' => MYTHEME_URI . '/assets/css/editor-styles.css'
    ),
    'quicktags'        => false,
    'dfw'              => false,
    'drag_drop_upload' => false
) );

有任何想法吗?

1 个回答
  • 解决方案可在/sf/ask/17360801/找到:

    function tinymce_paste_as_text( $init ) {
        $init['paste_as_text'] = true;
    
        // omit the pastetext button so that the user can't change it manually, current toolbar2 content as of 4.1.1 is "formatselect,underline,alignjustify,forecolor,pastetext,removeformat,charmap,outdent,indent,undo,redo,wp_help"
        $init["toolbar2"] = "formatselect,underline,alignjustify,forecolor,removeformat,charmap,outdent,indent,undo,redo,wp_help";
    
        return $init;
    }
    add_filter('tiny_mce_before_init', 'tinymce_paste_as_text');
    

    2022-12-31 10:04 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有