代码之家  ›  专栏  ›  技术社区  ›  Fredmat

强制纯文本粘贴

  •  2
  • Fredmat  · 技术社区  · 10 年前

    工作程序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'       => '<style>.mce-path { display: none !important; } </style>',
        'tinymce'          => array(
            'content_css' => MYTHEME_URI . '/assets/css/editor-styles.css'
        ),
        'quicktags'        => false,
        'dfw'              => false,
        'drag_drop_upload' => false
    ) );
    

    有什么想法吗?

    2 回复  |  直到 10 年前
        1
  •  12
  •   fiskhandlarn    9 年前

    在找到解决方案 https://stackoverflow.com/a/17421926/1109380 :

    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');
    
        2
  •  0
  •   Aibrean    10 年前

    您可以强制用户粘贴而不使用样式。我从Codex做了一些调整:

    function my_format_TinyMCE( $in ) {
        $in['remove_linebreaks'] = false;
        $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'] = 'none';
        $in['paste_text_use_dialog'] = true; // this is the one you REALLY want! 
        $in['wpeditimage_disable_captions'] = true;
        $in['plugins'] = 'tabfocus,paste,media,fullscreen,wordpress,wpeditimage,wpgallery,wplink,wpdialogs,wpfullscreen';
        $in['content_css'] = get_template_directory_uri() . "/editor-style.css";
        $in['wpautop'] = true;
        $in['apply_source_formatting'] = false;
        $in['toolbar1'] = 'spellchecker,wp_fullscreen ';
        $in['toolbar2'] = 'pastetext,removeformat,charmap,undo,redo,wp_help ';
        $in['toolbar3'] = '';
        $in['toolbar4'] = '';
        return $in;
    }
    add_filter( 'tiny_mce_before_init', 'my_format_TinyMCE' );
    

    未测试,但您可以在此处看到默认值: http://codex.wordpress.org/TinyMCE