如何限制WordPress中的个人信息长度和内容?

发布于 2024-09-14 07:53:28 字数 134 浏览 5 评论 0原文

在 WordPress 中,个人资料下有个人信息。我想阻止用户超过 400 个字符的最大长度。此外,他可以在简历信息中放置的超链接数量不应超过三个。我该怎么做?我对 JQuery 非常熟悉,如果这对这个问题有帮助的话。我只是 WordPress 的新手。

In WordPress there is Biographical Info under Profile. I would like to prevent the user from exceeding a maximum length of 400 characters. Also, the number of hyperlinks he can place in the biographical info should not exceed three. How do I do that? I am very familiar with JQuery, if that helps in this question. I am just a newbie with WordPress.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他夏了夏天 2024-09-21 07:53:28

对于 Javascript 端,您应该将必要的事件附加到 description 字段。您可以通过 wp_enqueue_script 挂钩加载脚本,您可能想要在 admin_enqueue_scripts 的处理程序中执行所有这些操作,您可以在其中检查传递的 $hook_name,在本例中是页面名称。当管理员编辑用户时,它是 user-edit.php;当用户编辑自己的信息时,它是 profile.php(在这种情况下,IS_PROFILE_PAGE > 也将被定义为 TRUE)。

add_action('admin_enqueue_scripts', 'add_description_validation_script');
function add_description_validation_script($pagename) {
   if ($pagename == 'profile.php' || $pagename == 'user-edit.php') {
      wp_enqueue_script('description_validation', '/path/to/description_validation.js');
   }
}

对于 PHP 端,您需要 pre_user_description 过滤器。这为您提供了当前的传记文本,您的函数可以更改它并返回其他内容。

add_filter('pre_user_description', 'sanitize_description');
function sanitize_description($description)
{
   // Do stuff with the description
   return $description;
}

如果您不想默默地更改描述,而是想显示错误,则应该查看 user_profile_update_errors 挂钩。在那里您可以验证给定的数据并向用户返回错误消息。

您可能希望将所有这些都封装在一个插件中,这样您就可以将代码放在一起并轻松启用或禁用它。插件只是 /wp-content/plugins/ 目录中的一个文件,很可能位于以您的插件命名的子目录中。

For the Javascript side, you should attach the necessary events to the description field. You can load your script via the wp_enqueue_script hook, and you probably want to do all this in your handler for admin_enqueue_scripts, where you check for the passed $hook_name, which in this case is the page name. It is user-edit.php when an admin edits a user, and profile.php when the user edits their own information (in which case IS_PROFILE_PAGE will also be defined as TRUE).

add_action('admin_enqueue_scripts', 'add_description_validation_script');
function add_description_validation_script($pagename) {
   if ($pagename == 'profile.php' || $pagename == 'user-edit.php') {
      wp_enqueue_script('description_validation', '/path/to/description_validation.js');
   }
}

For the PHP side, you need the pre_user_description filter. This gives you the current biographical text, and your function can change this and return something else.

add_filter('pre_user_description', 'sanitize_description');
function sanitize_description($description)
{
   // Do stuff with the description
   return $description;
}

If, instead of silently changing the description, you want to show an error, you should look into the user_profile_update_errors hook. There you can validate the given data and return error messages to the user.

You probably want to wrap this all up in a plugin, so you can keep the code together and easily enable or disable it. A plugin is just a file in the /wp-content/plugins/ directory, most likely in a subdirectory named after your plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文