[英]Customizing user_nicename in wp_users table in wordpress
默认情况下,wordpress会将写在wp_users
表中的user_nicename
列的内容user_nicename
wp_users
。 它将删除空格,一些特殊字符并将大写字母更改为小写字母。 无需清理就可以更新user_nicename
列吗?
可以使用pre_user_nicename过滤器。 您可以在这里阅读https://developer.wordpress.org/reference/hooks/pre_user_nicename/在对nicename进行清理之后立即应用该过滤器,但是我们仍然可以访问未经清理的数据。 这是从wp-includes / user.php
/*
* If a nicename is provided, remove unsafe user characters before using it.
* Otherwise build a nicename from the user_login.
*/
if ( ! empty( $userdata['user_nicename'] ) ) {
$user_nicename = sanitize_user( $userdata['user_nicename'], true );
if ( mb_strlen( $user_nicename ) > 50 ) {
return new WP_Error( 'user_nicename_too_long', __( 'Nicename may not be longer than 50 characters.' ) );
}
} else {
$user_nicename = mb_substr( $user_login, 0, 50 );
}
$user_nicename = sanitize_title( $user_nicename );
// Store values to save in user meta.
$meta = array();
/**
* Filters a user's nicename before the user is created or updated.
*
* @since 2.0.3
*
* @param string $user_nicename The user's nicename.
*/
$user_nicename = apply_filters( 'pre_user_nicename', $user_nicename );
$raw_user_url = empty( $userdata['user_url'] ) ? '' : $userdata['user_url'];
您仍然可以使用$ userdata ['user_nicename']变量来访问用户输入的未修改名称。 因此,示例过滤器将如下所示:
add_filter( 'pre_user_nicename', 'my_nicename_modification');
function my_nicename_modification($userdata) {
/*do anything you want with $userdata['user_nicename'] here
or leave blank if you want it saved just as the user typed it in */
return $userdata['user_nicename'];
}
这将停止$user_nicename = sanitize_title( $user_nicename );
从运行开始,您可以在这里阅读更多内容https://codex.wordpress.org/Function_Reference/sanitize_title 。 这就是您在问题中解释的名称的修改。 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.