wordpress 的里的所有注册用户都有一个专属的链接,称之为作者存档页链接,通常是这样的:
// 未 url 重写 https://www.pc528.net/?author=1 // 已 url 重写 https://www.pc528.net/author/admin
这样就存在很大的安全隐患,这样就直接暴露了登录 WordPress 的用户名,特别是管理员的用户名!下面一个不错的方法的解决方法是将 WordPress 作者存档链接中的用户名改为昵称,方法如下:
1、修改用户名昵称,默认的昵称显示的是用户名,你可以修改成一个中文昵称,并设置公开显示显示为昵称。
2、在 functions.php 中加入以下代码:
/** * WordPress 修改author作者存档链接中的个人主页用户名改为昵称 隐藏管理员用户名-(不忘初心系统博客) * https://www.pc528.net/wordpress-author.html */ //使用昵称替换用户名,通过用户 ID 进行查询 add_filter( 'request', 'lxtx_wpdaxue_request' ); function lxtx_wpdaxue_request( $query_vars ) { if ( array_key_exists( 'author_name', $query_vars ) ) { global $wpdb; $author_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key='nickname' AND meta_value = %s", urldecode( $query_vars['author_name'] ) ) ); if ( $author_id ) { $query_vars['author'] = $author_id; unset( $query_vars['author_name'] ); } } return $query_vars; } //使用昵称替换链接中的用户名 add_filter( 'author_link', 'lxtx_wpdaxue_author_link', 10, 3 ); function lxtx_wpdaxue_author_link( $link, $author_id, $author_nicename ) { $author_nickname = get_user_meta( $author_id, 'nickname', true ); if ( $author_nickname ) { $link = str_replace( $author_nicename, $author_nickname, $link ); } return $link; }