首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法在wp_editor函数中应用字符限制?

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

Stack Overflow用户
提问于 2012-09-21 21:00:17
回答 2查看 4.5K关注 0票数 2

我试图找出如何组合在一个前端提交表单的资源框中的代码,但我需要它被限制在一定数量的字符。

代码语言:javascript
复制
<?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']; } } ?>

下面的代码检查字段是否为空:

代码语言:javascript
复制
<?php if(isset($_POST['resource'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['resource']); } else { echo $_POST['resource']; } } ?>

如何在wp_editor函数中进行检查?我需要这个,以允许和简化我的资源框内的html给用户…

这是我在内部使用的javascript函数('onkeyup'=>'countChar( This )'),但它不起作用。

我要掉下去了吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-03 04:14:34

我根据自己的需要对Bryan Gentry的解决方案做了一点改动,效果很好。如果您需要类似的功能,下面是我的代码。

代码语言:javascript
复制
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;
}
票数 0
EN

Stack Overflow用户

发布于 2012-09-30 12:39:37

首先,您需要更正wp_editor的实现。你的代码应该是这样的:

代码语言:javascript
复制
<?php wp_editor( get_option('resource'), 'resource', array('textarea_name' => 'resource', 'editor_class'=>'requiredField', 'textarea_rows'=>'6', 'media_buttons' => false)  );?>

请注意,the wp_editor function不接受javascript方法的参数,并且有一个参数用于'editor_class‘,但没有'class’本身。

接下来,需要将keyup事件(或keydown事件)绑定到TinyMCE编辑器。这需要在编辑器初始化时完成。This other Q&A帮我找到了解决方案。您需要在functions.php文件中添加与以下内容非常类似的内容:

代码语言:javascript
复制
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;
    }

这将更改TinyMCE初始化,以便为您添加事件。当事件被触发(按下一个键)时,该函数将检查活动编辑器的ID,以查看它是否是您希望限制其字符数的编辑器之一。然后,它获取编辑器的内容并将其传递给countChar函数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12531113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档