使显示名称唯一


13

现在,您可以根据需要设置显示名称,甚至可以模拟管理员。显示名称是否可以唯一?

Answers:


7

据我所知,您所能做的就是通过过滤显示名称pre_user_display_name并检查它是否已经存在。不幸的WP_User_Query是不支持by的查询display_name,因此我们还必须通过将其添加到WHERE子句中pre_user_query。另外,我无法想到一种优雅的方式来处理display_name已经存在的情况,而不会返回任何内容,因为我们无法通过过滤器知道该潜在显示名称附加到了哪个用户。也许我只是累了,错过了明显的东西!无论如何,这是我创建的一个快速测试:

class wpa82239_display_name {
    private $display_name;

    public function __construct(){
        add_filter( 'pre_user_display_name', array( $this, 'display_name_filter' ) );
    }

    public function display_name_filter( $display_name ){

        $this->display_name = $display_name;
        add_action( 'pre_user_query', array( $this, 'user_query_filter' ) );
        $args = array(
            'number' => 1,
            'fields' => 'ID'
        );
        $user_search = new WP_User_Query( $args );
        remove_action( 'pre_user_query', array( $this, 'user_query_filter' ) );

        if( 0 == $user_search->total_users )
            return $display_name;

        return null;
    }

    public function user_query_filter( $query ){
        global $wpdb;
        $query->query_where .= $wpdb->prepare(
            " AND $wpdb->users.display_name = %s",
            $this->display_name
        );
    }

}
$wpa82239_display_name = new wpa82239_display_name();

嘿,米洛,感谢您的解决方案,但是如果只是取消设置display_name,那将是没有用的。在这个新的一天有什么想法吗?
FLX

我不会说这完全没用。在显示前端,如果显示名称为空,则可以默认登录;在后端,可以通过admin_footer添加一些JavaScript,以发出警告,指出它们必须选择唯一的显示名称。
米洛

0

它很脏,但是可以完成工作,只需将其放在单个站点上的主题functions.php中,或放在多站点的mu-plugins文件夹内的functions.php文件中。

该脚本还将扫描现有用户(如果您的站点已经有很多用户),并且任何非唯一的显示名称都将被更新并替换为用户名。

我知道这并不完全理想,但是总是欢迎您提出任何与现有用户打交道或进行改进的建议。

 
    // Make nickname & display_name unique
    // and automatically change non unique nicks & display name to username
    // in case you already have existing users
    // by Ashok & Vaughan Montgomery
    /*
     * adding action when user profile is updated
     */
    add_action('personal_options_update', 'check_display_name');
    add_action('edit_user_profile_update', 'check_display_name');
    function check_display_name($user_id) {
            global $wpdb;
        // Getting user data and user meta data
            $err['display'] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->users WHERE display_name = %s AND ID <> %d", $_POST['display_name'], $_POST['user_id']));
        $err['nick'] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->users as users, $wpdb->usermeta as meta WHERE users.ID = meta.user_id AND meta.meta_key = 'nickname' AND meta.meta_value = %s AND users.ID <> %d", $_POST['nickname'], $_POST['user_id']));
        foreach($err as $key => $e) {
            // If display name or nickname already exists
            if($e >= 1) {
                $err[$key] = $_POST['username'];
                // Adding filter to corresponding error
                add_filter('user_profile_update_errors', "check_{$key}_field", 10, 3);
            }
        }
    }
    /*
     * Filter function for display name error
     */
    function check_display_field($errors, $update, $user) {
            $errors->add('display_name_error',('Sorry, Display Name is already in use. It needs to be unique.'));
            return false;
    }
    /*
     * Filter function for nickname error
     */
    function check_nick_field($errors, $update, $user) {
            $errors->add('display_nick_error',('Sorry, Nickname is already in use. It needs to be unique.'));
            return false;
    }
    /*
     * Check for duplicate display name and nickname and replace with username
     */
    function display_name_and_nickname_duplicate_check() {
        global $wpdb;
        $query = $wpdb->get_results("SELECT * FROM $wpdb->users");
        $query2 = $wpdb->get_results("SELECT * FROM $wpdb->users as users, $wpdb->usermeta as meta WHERE users.ID = meta.user_id AND meta.meta_key = 'nickname'");
        $c = count($query);
        for($i = 0; $i < $c; $i++) {
            for($j = $i+1; $j < $c; $j++) {
                if($query[$i]->display_name == $query[$j]->display_name){
                    wp_update_user(
                            array(
                                  'ID' => $query[$i]->ID,
                                  'display_name' => $query[$i]->user_login
                            )
                        );
                }
                if($query2[$i]->meta_value == $query2[$j]->meta_value){
                    update_user_meta($query2[$i]->ID, 'nickname', $query2[$i]->user_login, $prev_value);
                }
            }
        }
    }
    // Call the function
    display_name_and_nickname_duplicate_check();

/*
 * Calling the display_name_and_nickname_duplicate_check() again when a new user is registered
 */
add_action( 'user_register', 'check_nickname', 10, 1 );
function check_nickname() {
    display_name_and_nickname_duplicate_check();
}


0

尝试这个:

add_action('personal_options_update', 'check_display_name');
add_action('edit_user_profile_update', 'check_display_name');
function check_display_name($user_id) {
global $wpdb;
// Getting user data and user meta data
$err['display'] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->users WHERE display_name = %s AND ID <> %d", $_POST['display_name'], $_POST['user_id']));
$err['nick'] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->users as users, $wpdb->usermeta as meta WHERE users.ID = meta.user_id AND meta.meta_key = 'nickname' AND meta.meta_value = %s AND users.ID <> %d", $_POST['nickname'], $_POST['user_id']));
foreach($err as $key => $e) {
// If display name or nickname already exists
if($e >= 1) {
$err[$key] = $_POST['username'];
// Adding filter to corresponding error
add_filter('user_profile_update_errors', "check_{$key}_field", 10, 3);
}
}
}
/*
* Filter function for display name error
*/
function check_display_field($errors, $update, $user) {
$errors->add('display_name_error',__('Sorry, Display Name is already in use. It needs to be unique.'));
return false;
}
/*
* Filter function for nickname error
*/
function check_nick_field($errors, $update, $user) {
$errors->add('display_nick_error',__('Sorry, Nickname is already in use. It needs to be unique.'));
return false;
}
/*
* Check for duplicate display name and nickname and replace with username
*/
function display_name_and_nickname_duplicate_check() {
global $wpdb;
$query = $wpdb->get_results("select * from $wpdb->users");
$query2 = $wpdb->get_results("SELECT * FROM $wpdb->users as users, $wpdb->usermeta as meta WHERE users.ID = meta.user_id AND meta.meta_key = 'nickname'");
$c = count($query);
for($i = 0; $i < $c; $i++) {
for($j = $i+1; $j < $c; $j++) {
if($query[$i]->display_name == $query[$j]->display_name){
wp_update_user(
array(
'ID' => $query[$i]->ID,
'display_name' => $query[$i]->user_login
)
);
}
if($query2[$i]->meta_value == $query2[$j]->meta_value){
update_user_meta($query2[$i]->ID, 'nickname', $query2[$i]->user_login, $prev_value);
}
}
}
}
// Call the function
display_name_and_nickname_duplicate_check();

/*
* Calling the display_name_and_nickname_duplicate_check() again when a new user is registered
*/
add_action( 'user_register', 'check_nickname', 10, 1 );
function check_nickname() {
display_name_and_nickname_duplicate_check();
}

链接:http//bappi-d-great.com/unique-display-name-and-nickname-in-wordpress/

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.