如何在WordPress编辑器中限制提取的文本长度?

时间:2014-04-19 10:11:56

标签: wordpress filter

我需要在WordPress中编写新帖子时限制摘录字段的长度。当达到字符或单词的限制时,我想显示错误消息并阻止用户写更多输入。

我发现了一些提示,但实际的Wordpress版本没有。我应该从哪里开始调查,是否有过滤器来输出自定义摘录字段?

1 个答案:

答案 0 :(得分:0)

在tinymce Script中使用此代码在Them按钮下。

和“id_txt”是您的文字区域ID

setup : function(ed) {
ed.onKeyUp.add(function(ed, e) {
txt = tinyMCE.activeEditor.getContent();

var strip = (tinyMCE.activeEditor.getContent()).replace(/(<([^>]+)>)/ig,””);
var text =  strip.length + ” Characters”
tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + ‘_path_row’), text);

id_txt = tinyMCE.activeEditor.id;
//alert (id_txt);
//strip the html
txt = txt.replace(/(<([^>]+)>)/ig,””);
txt = txt.replace(/&nbsp;/g,””);
if (id_txt == ‘blog_content’)
{

if (txt.length > 1000)
{
$(“#ErrorMsg”).css(‘display’,’block’);
$(“#ErrorMsg”).html(‘Please Enter Only 1000 Charecters !’);
txt = txt.substring(0,1000);
text =  1000 + ” Characters”;
tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + ‘_path_row’), text);
tinyMCE.activeEditor.setContent(txt);
ed.selection.select(ed.getBody(), true);
ed.selection.collapse(false);
return false;
}
}

/// Condition for Header Qutes

if (id_txt == ‘header_content’)
{
if (txt.length > 150)
{
$(“#ErrorMsg”).css(‘display’,’block’);
$(“#ErrorMsg”).html(‘Please Enter Only 150 Charecters !’);
txt = txt.substring(0,150);
text =  150 + ” Characters”;
tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + ‘_path_row’), text);
tinyMCE.activeEditor.setContent(txt);
ed.selection.select(ed.getBody(), true);
ed.selection.collapse(false);
return false;
}
}

});
}!