简体   繁体   English

有没有办法在wp_editor函数中应用字符限制?

[英]Is there a way to apply characters limit inside wp_editor function?

I am trying to find out how to combine this code in resource box on a front end submission form but I need it to be limited to a certain number of characters. 我试图找出如何在前端提交表单上的资源框中组合此代码,但我需要将其限制为一定数量的字符。

<?php wp_editor( get_option('resource'), 'resource', array('textarea_name' => 'resource', 'class'=>'requiredField', 'textarea_rows'=>'6','id'=>'resource' ,'onkeyup'=>'countChar(this)','media_buttons' => false)  );?><?php if(isset($_POST['resource'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['resource']); } else { echo $_POST['resource']; } } ?>

This code checks if the field is empty: 此代码检查该字段是否为空:

<?php if(isset($_POST['resource'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['resource']); } else { echo $_POST['resource']; } } ?>

How can I make that check inside wp_editor function? 我如何在wp_editor函数中进行检查? I need this to allow and simplify html inside my resource box to users... 我需要这个来允许并简化用户资源框中的html ...

This is the javascript function that I am using ('onkeyup'=>'countChar(this)') inside but it's not working. 这是我在内部使用的JavaScript函数('onkeyup'=>'countChar(this)'),但无法正常工作。

Here Am I falling? 我在这里跌倒吗?

First of all, you need to correct your implementation of the wp_editor. 首先,您需要更正wp_editor的实现。 Your code should read something like this: 您的代码应如下所示:

<?php wp_editor( get_option('resource'), 'resource', array('textarea_name' => 'resource', 'editor_class'=>'requiredField', 'textarea_rows'=>'6', 'media_buttons' => false)  );?>

Note that the wp_editor function does not accept arguments for javascript methods, and that there is a parameter for 'editor_class' but not 'class' on its own. 请注意, wp_editor函数不接受javascript方法的参数,并且有一个用于“ editor_class”的参数,而不是“ class”本身。

Next, you need to bind the keyup event (or keydown event) to the TinyMCE editor. 接下来,您需要将keyup事件(或keydown事件)绑定到TinyMCE编辑器。 This needs to be done upon the initialization of the editor. 这需要在编辑器初始化时完成。 This other Q&A helped me find the solution to that for you. 其他问答环节也帮助我为您找到了解决方案。 You need to add something very much like the following to your functions.php file: 您需要在functions.php文件中添加类似于以下内容的内容:

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=='resource') || (tinyMCE.activeEditor.editorId=='the_other_editor_id_you_want_to_limit') {
                        countChar(tinyMCE.activeEditor.getContent());
                     }
                });
            }][0]
            JS;
    return $initArray;
    }

This will change the TinyMCE initialization to add the event for you. 这将更改TinyMCE初始化以为您添加事件。 When the event is fired (a key is pressed) the function will check the ID of the active editor to see whether it is one of the editors whose character count you wish to limit. 当事件被触发时(按下一个键),该功能将检查活动编辑器的ID,以查看它是否是您希望限制其字符数的编辑器之一。 Then it grabs the content of the editor and passes it into your countChar function. 然后,它获取编辑器的内容,并将其传递给您的countChar函数。

I altered Bryan Gentry's solution a tiny bit to my needs and it works nicely. 我根据自己的需要对Bryan Gentry的解决方案进行了改动,并且效果很好。 Here is my code if you need a similar functionality. 如果您需要类似的功能,这是我的代码。

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;
}

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

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