WordPress教程

wordpress支持中文用户名的简便方法

阿里云

  今天创客云介绍新的方法,借鉴了 wp-includes/formatting.php 中 sanitize_user 函数的写法,同样是将以下 php 代码复制到当前主题目录下的 functions.php 中,即可让 WordPress 支持使用中文用户名注册和登录:

代码如下:

function ludou_sanitize_user ($username, $raw_username, $strict) {
  $username = wp_strip_all_tags( $raw_username );
  $username = remove_accents( $username );
  // Kill octets
  $username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
  $username = preg_replace( '/&.+?;/', '', $username ); // Kill entities

  // 网上很多教程都是直接将$strict 赋值 false,
  // 这样会绕过字符串检查,留下隐患
  if ($strict) {
    $username = preg_replace ('|[^a-z\p{Han}0-9 _.\-@]|iu', '', $username);
  }

  $username = trim( $username );
  // Consolidate contiguous whitespace
  $username = preg_replace( '|\s+|', ' ', $username );

  return $username;
}

add_filter ('sanitize_user', 'ludou_sanitize_user', 10, 3);

wordpress 支持中文用户名的简便方法

已有 548 人购买
查看演示升级 VIP立刻购买

也想出现在这里?联系我们
创客主机
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!