在WordPress编辑器中如何限制字段的文本长度? [英] How to restrict text length of a field while in WordPress editor?

查看:109
本文介绍了在WordPress编辑器中如何限制字段的文本长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WordPress中创建新帖子时限制字段。标题不得超过40个字符。对于内容,不得超过400个字符。如果超过了这些最大值,我想显示一条错误消息,不要让用户继续。

I would like to restrict the fields while creating a new post in WordPress. For the title, it should not exceed 40 characters. For the content, it should not exceed 400 characters. If these maximum values are exceeded, I would like to show an error message and not let the user continue. How do I do that in WordPress?

推荐答案

您应该能够使用wordpress过滤器来修改在编辑器被调用。本质上,您将希望使用它插入一些javascript和一个额外的div标签来显示错误,然后仅读取 editorcontainer id的内容,并在达到某个字符限制后显示错误。

You should be able to use wordpress filters to modify the code that gets outputted when the editor is called. Essentially you would want to use it to insert some javascript and an extra div tag to display your error, then just read the contents of the "editorcontainer" id and show the error once it reaches a certain character limit.

我现在没有时间写案例,但是根据您的技能水平,您正在寻找的功能是:

I don't have the time at the moment to write a case example, but depending on your skill level, the functions you are looking for are:

 apply_filters("the_editor", "customfunction_limitarea");

其中customfunction_limit区域是您创建的用于插入javascript的函数。您可以在第1822行的 wp-includes\general-template.php中看到当前如何调用the_editor以及如何应用默认过滤器。默认看起来像这样:

Where customfunction_limit area is your created function to insert the javascript. You can see how the_editor is currently called and how the default filters are applied in "wp-includes\general-template.php" on line 1822. The default looks like this:

$the_editor = apply_filters('the_editor', "<div id='editorcontainer'><textarea rows='$rows'$class cols='40' name='$id' tabindex='$tab_index' id='$id'>%s</textarea></div>\n");

我会尝试通过将新过滤器放置在主题中的functions.php文件中来修改该语句目录,这样您就不必担心更新期间它会被覆盖。否则,如果您绝对必须编辑wordpress核心(通常是不行),我认为可以使用general_template.php。

I would try modifying that statement by placing a new filter in a functions.php file located in your themes directory, that way you don't have to worry about it getting over-written during an update. Otherwise, if you absolutely have to edit the wordpress core (generally a no-no), general_template.php would be the place to do it I think.

基本上只是阅读稍微增加一下wordpress过滤器(请注意,除了基本的东西以外,没有大量的文档或示例可用),并且应该可以提供所需的一切。输入验证端很容易找到脚本,只是google jquery帖子限制。

Essentially just read up on wordpress filters a little bit (be warned there's not a ton of documentation or examples available for it other than the basic stuff), and that should provide everything you need. The input verification end is easy enough to find scripts, just google jquery post limiting. Something like this might be exactly what your looking for:

http://swiki.fromdev.com/2010/02/jquery-maximum-limit-texttextarea-with.html

这篇关于在WordPress编辑器中如何限制字段的文本长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆