gpt4 book ai didi

mysql - 在 wordpress 的 wp_users 表中自定义 user_nicename

转载 作者:行者123 更新时间:2023-11-29 03:22:23 26 4
gpt4 key购买 nike

默认情况下,wordpress 会清理写入 wp_users 表中的 user_nicename 列的内容。它将删除空格、一些特殊字符并将大写字母更改为小写字母。是否可以在不进行清理的情况下更新 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'] 变量访问用户输入的未修改的 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 .正如您在问题中解释的那样,这就是修改 nicename 。希望这有帮助

关于mysql - 在 wordpress 的 wp_users 表中自定义 user_nicename,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42018867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com