在使用WordPress搭建网站时,管理员的用户名安全是一个需要重视的问题。如果不加以注意,用户名可能会被暴露,给网站安全带来隐患。以下是一个详细的教程,教你如何避免暴露WordPress管理员登录用户名。
![图片[1]_避免暴露你的WordPress管理员登录用户名的详细教程_知途无界](https://zhituwujie.com/wp-content/uploads/2024/10/d2b5ca33bd20241015094205.png)
一、背景介绍
在研究网站评论结构或查看网页源代码时,你可能会无意间发现自己的管理员用户名被暴露。这通常发生在WordPress的评论部分或用户页面中。一旦用户名被暴露,黑客可能会利用这一信息进行暴力破解,尝试登录你的网站后台。
二、解决步骤
步骤1:将作者存档链接中的用户名更改为用户ID
- 参考文章:你可以参考这篇文章来了解如何将作者存档链接中的用户名更改为用户ID:使用用户ID作为作者归档链接的slug。
- 操作步骤:
- 登录到你的WordPress后台。
- 进入“设置”->“固定链接”页面。
- 在“可选”部分找到“作者归档的基础结构”选项。
- 将默认的用户名替换为用户ID(通常是通过插件或手动编辑代码实现)。
- 保存更改。
步骤2:修改WordPress源代码以隐藏用户名
由于WordPress的某些函数可能会直接输出用户名,我们需要修改这些函数的源代码来隐藏用户名。以下是几种方法:
方法1:直接修改WordPress程序
- 查找并修改相关函数:
get_comment_class()
函数在wp-includes/comment-template.php
文件中。你需要找到这个函数,并将其中输出用户名的部分(通常是$user->user_nicename
)改为$user->user_id
。get_body_class()
函数在wp-includes/post-template.php
文件中。同样地,你需要找到这个函数,并删除或注释掉其中输出用户名的部分(通常是$author->user_nicename
)。
- 注意事项:直接修改WordPress程序源代码有一个缺点,即每次更新WordPress程序时都需要重新进行这样的修改。因此,这种方法虽然有效,但不够方便。
方法2:使用functions.php文件过滤掉相关class
- 添加过滤函数:
- 登录到你的WordPress后台。
- 进入“外观”->“编辑器”页面。
- 找到并打开你的主题目录下的
functions.php
文件。 - 在文件末尾添加以下代码:
function lxtx_remove_comment_body_author_class($classes) {foreach ($classes as $key => $class) {if (strstr($class, "comment-author-") || strstr($class, "author-")) {unset($classes[$key]);}}return $classes;}add_filter('comment_class', 'lxtx_remove_comment_body_author_class');add_filter('body_class', 'lxtx_remove_comment_body_author_class');function lxtx_remove_comment_body_author_class($classes) { foreach ($classes as $key => $class) { if (strstr($class, "comment-author-") || strstr($class, "author-")) { unset($classes[$key]); } } return $classes; } add_filter('comment_class', 'lxtx_remove_comment_body_author_class'); add_filter('body_class', 'lxtx_remove_comment_body_author_class');function lxtx_remove_comment_body_author_class($classes) { foreach ($classes as $key => $class) { if (strstr($class, "comment-author-") || strstr($class, "author-")) { unset($classes[$key]); } } return $classes; } add_filter('comment_class', 'lxtx_remove_comment_body_author_class'); add_filter('body_class', 'lxtx_remove_comment_body_author_class');
这段代码会过滤掉 comment_class()
和 body_class()
函数中输出的包含 comment-author-
和 author-
的class,从而隐藏用户名。
- 保存更改:点击“更新文件”按钮保存你的更改。
方法3:改为输出用户ID或用户昵称(优化版)
- 添加自定义函数:
- 同样地,在
functions.php
文件中添加以下代码:
- 同样地,在
function lxtx_change_comment_or_body_classes($classes, $comment_id) {global $wp_query;$comment = get_comment($comment_id);$user = get_userdata($comment->user_id);$comment_author = 'comment-author-'.sanitize_html_class($user->user_nicename, $comment->user_id);$author = $wp_query->get_queried_object();$archive_author = 'author-'.sanitize_html_class($author->user_nicename, $author->ID);foreach ($classes as $key => $class) {switch ($class) {case $comment_author:$classes[$key] = 'comment-author-'.sanitize_html_class($comment->user_id);break;case $archive_author:$classes[$key] = 'author-'.sanitize_html_class($author->ID);break;function lxtx_change_comment_or_body_classes($classes, $comment_id) { global $wp_query; $comment = get_comment($comment_id); $user = get_userdata($comment->user_id); $comment_author = 'comment-author-'.sanitize_html_class($user->user_nicename, $comment->user_id); $author = $wp_query->get_queried_object(); $archive_author = 'author-'.sanitize_html_class($author->user_nicename, $author->ID); foreach ($classes as $key => $class) { switch ($class) { case $comment_author: $classes[$key] = 'comment-author-'.sanitize_html_class($comment->user_id); break; case $archive_author: $classes[$key] = 'author-'.sanitize_html_class($author->ID); break;function lxtx_change_comment_or_body_classes($classes, $comment_id) { global $wp_query; $comment = get_comment($comment_id); $user = get_userdata($comment->user_id); $comment_author = 'comment-author-'.sanitize_html_class($user->user_nicename, $comment->user_id); $author = $wp_query->get_queried_object(); $archive_author = 'author-'.sanitize_html_class($author->user_nicename, $author->ID); foreach ($classes as $key => $class) { switch ($class) { case $comment_author: $classes[$key] = 'comment-author-'.sanitize_html_class($comment->user_id); break; case $archive_author: $classes[$key] = 'author-'.sanitize_html_class($author->ID); break;
© 版权声明
文中内容均来源于公开资料,受限于信息的时效性和复杂性,可能存在误差或遗漏。我们已尽力确保内容的准确性,但对于因信息变更或错误导致的任何后果,本站不承担任何责任。如需引用本文内容,请注明出处并尊重原作者的版权。
THE END
暂无评论内容