在wordpress中的wp_users表中自定义user_nicename

时间:2017-02-03 07:24:22

标签: mysql sql wordpress

默认情况下,wordpress会将写入user_nicename表中wp_users列的内容放大。它将删除空格,一些特殊字符并将大写更改为小写。是否可以在没有清理的情况下更新user_nicename列?

1 个答案:

答案 0 :(得分:0)

可以使用 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。希望这有帮助