【问题标题】:Customizing user_nicename in wp_users table in wordpress在 wordpress 的 wp_users 表中自定义 user_nicename
【发布时间】:2017-06-20 12:10:30
【问题描述】:

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

【问题讨论】:

  • 嗨 :) 规格是什么,你想做什么。
  • 我想要未经处理的 nice_name。

标签: mysql sql wordpress


【解决方案1】:

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

【讨论】:

  • function my_nicename_modification($userdata['user_nicename']) { } 抛出错误。我没有在这里得到未经处理的 nicename。
  • 我们在过滤器中得到了净化的 nicename,而不是 $userdata 变量。
  • 你是对的,我们无法从那里访问 $userdata 变量,而且我之前似乎找不到访问它的操作或过滤器。好奇别人是否发现了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 2013-07-25
  • 2014-08-13
相关资源
最近更新 更多