简体   繁体   English

WordPress所见即所得编辑器的字符限制

[英]Character limitation for WordPress WYSIWYG editor

I limited the number of characters that can be added as content for a special page (event submission page). 我限制了可以作为特殊页面(事件提交页面)的内容添加的字符数。 It works fine in text or code mode in WordPress but not when I use the WYSIWYG editor. 它在WordPress的文本或代码模式下工作正常,但在使用WYSIWYG编辑器时却不能。

Any idea how to change it so it also works using the WordPress editor? 知道如何更改它,使其也可以使用WordPress编辑器吗?

Thank you so much! 非常感谢!

Here is the JS I am using. 这是我正在使用的JS。

     // Set your character limit
     var count_limit = 1000;

     // Set the initial symbol count on page load
     $('#tcepostcontent-wc span').html($('#tcepostcontent').val());

     $('#tcepostcontent').on('keyup', function () {
      var char_count = $(this).val().length;
      var tcepostcontent = $(this).val();
      var text_remaining = count_limit - char_count;

     // Update the character count on every key up
     $('#tcepostcontent-wc span').html(text_remaining);

     if (char_count >= count_limit) {
       $('#tcepostcontent-wc').css('color', 'red');
       $(this).val(tcepostcontent.substr(1, count_limit));
     } else {
       $('#tcepostcontent-wc').css('color', null);
     }

     }).after('<p id="tcepostcontent-wc">Max 1000 are available <span>1000</span></p>');

The visual editor of WordPress is TinyMCE and he implement a custom API there you can use to solve this topic. WordPress的可视编辑器是TinyMCE,他实现了自定义API,您可以在其中使用该API来解决该主题。 You should use the follow source, add it in a small custom plugin, change the id for tinyMCE.activeEditor.editorId of the editor, activate it, and done. 您应该使用跟随源,将其添加到一个小的自定义插件中,更改编辑器的tinyMCE.activeEditor.editorId的ID,激活它并完成。

add_filter( 'tiny_mce_before_init', 'wpse24113_tiny_mce_before_init' );
function wpse24113_tiny_mce_before_init( $initArray ) {

    $initArray['setup'] = <<<JS
[function( ed ) {
    ed.onKeyDown.add( function( ed, e ) {
        if ( tinyMCE.activeEditor.editorId == 'content-id' ) {

            var content = tinyMCE.activeEditor.getContent();
            var max = 300;
            var len = content.length;

            if (len >= max) {
              $( '#charNum' ).html( '<span class="text-error">You've got more then '+max+' characters!</span>' );
            } else {
             var charCount = max - len;

             $( '#charNum').html( charCount + ' characters left' );
            }
         }
    });

}][0]
JS;

    return $initArray;
}

The source is from this answer in the SE Forum for WordPress topics. 来源是来自SE论坛中有关WordPress主题的答案

It works in text mode but not for the visual (here "visuell) "one. 它以文本模式工作,但不适用于视觉模式(此处为“ visuell”)。

It should be just a matter of picking the right id's right but whatever I pick (#tinymce, tcepostcontent_ifr etc.) it does not work. 这应该只是选择正确的ID的问题,但是无论我选择什么(#tinymce,tcepostcontent_ifr等),它都不起作用。

Thanks a lot for your help! 非常感谢你的帮助!

Cheers, Torsten 干杯,托尔斯滕

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM