
在 WordPress 中,您可以注册多个作者,每个作者都有自己的 URL。问题是这个作者 URL 会显示作者的用户名,这就给您的 WordPress 网站带来了安全风险。由于作者用户名暴露在外,攻击者可以利用它来尝试登录或强行进入您的网站。
为了解决这个问题,我们可以用 UUID 这样的随机 ID 屏蔽作者 URL。这样,作者 URL 就不会暴露作者的用户名,而且会更加安全。
我们将研究两种方法:硬办法,我们自己编写代码;简易法,我们使用插件。
话不多说,让我们来看看它是如何工作的。
硬办法(写代码)
首先,在 /wp-content/plugin
目录或 /wp-content/mu-plugins/
目录下创建一个新的 PHP 文件,例如 uuid-slug.php
,将其作为必须使用的插件加载。该文件将包含插件头文件…
* This file is read by WordPress to display the plugin's information in the admin area.
* Plugin Name: Author UUID Slug
* Plugin URI: https://github.com/hongkiat/wp-author-uuid-slug
* Description: Use UUID for the author URL.
* Author URI: https://github.com/tfirdaus
/**
* Plugin bootstrap file.
*
* This file is read by WordPress to display the plugin's information in the admin area.
*
* @wordpress-plugin
* Plugin Name: Author UUID Slug
* Plugin URI: https://github.com/hongkiat/wp-author-uuid-slug
* Description: Use UUID for the author URL.
* Version: 1.0.0
* Requires at least: 6.0
* Requires PHP: 7.4
* Author: Thoriq Firdaus
* Author URI: https://github.com/tfirdaus
*/
/**
* Plugin bootstrap file.
*
* This file is read by WordPress to display the plugin's information in the admin area.
*
* @wordpress-plugin
* Plugin Name: Author UUID Slug
* Plugin URI: https://github.com/hongkiat/wp-author-uuid-slug
* Description: Use UUID for the author URL.
* Version: 1.0.0
* Requires at least: 6.0
* Requires PHP: 7.4
* Author: Thoriq Firdaus
* Author URI: https://github.com/tfirdaus
*/
…以及实现基于 UUID 的作者 URL 所需的逻辑。在这种情况下,我们将在用户配置文件编辑器中提供一个简单的输入来添加 UUID。
add_action('show_user_profile', 'add_uuid_field_to_profile');
add_action('edit_user_profile', 'add_uuid_field_to_profile');
function add_uuid_field_to_profile($user)
$uuid = get_user_meta($user->ID, '_uuid', true);
<table class="form-table">
<th><label for="user_uuid"><?php esc_html_e('UUID', 'hongkiat'); ?></label></th>
value="<?php echo esc_attr($uuid); ?>"
<?php echo !current_user_can('manage_options') ? 'readonly' : ''; ?>
if (current_user_can('manage_options')) {
esc_html_e('Enter or update the UUID for this user.', 'hongkiat');
esc_html_e('This UUID is read-only for non-administrators.', 'hongkiat');
add_action('personal_options_update', 'save_uuid_field');
add_action('edit_user_profile_update', 'save_uuid_field');
function save_uuid_field($user_id)
if (!current_user_can('manage_options', $user_id)) {
$new_uuid = isset($_POST['user_uuid']) ? sanitize_text_field($_POST['user_uuid']) : '';
if (!empty($new_uuid) && is_uuid($new_uuid)) {
update_user_meta($user_id, '_uuid', $new_uuid);
delete_user_meta($user_id, '_uuid');
$pattern = '/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i'; // UUID pattern.
return (bool) preg_match($pattern, $value);
add_action('show_user_profile', 'add_uuid_field_to_profile');
add_action('edit_user_profile', 'add_uuid_field_to_profile');
function add_uuid_field_to_profile($user)
{
$uuid = get_user_meta($user->ID, '_uuid', true);
?>
<table class="form-table">
<tr>
<th><label for="user_uuid"><?php esc_html_e('UUID', 'hongkiat'); ?></label></th>
<td>
<input
type="text"
name="user_uuid"
id="user_uuid"
value="<?php echo esc_attr($uuid); ?>"
class="regular-text"
<?php echo !current_user_can('manage_options') ? 'readonly' : ''; ?>
/>
<p class="description">
<?php
if (current_user_can('manage_options')) {
esc_html_e('Enter or update the UUID for this user.', 'hongkiat');
} else {
esc_html_e('This UUID is read-only for non-administrators.', 'hongkiat');
}
?>
</p>
</td>
</tr>
</table>
<?php
}
add_action('personal_options_update', 'save_uuid_field');
add_action('edit_user_profile_update', 'save_uuid_field');
function save_uuid_field($user_id)
{
if (!current_user_can('manage_options', $user_id)) {
return false;
}
$new_uuid = isset($_POST['user_uuid']) ? sanitize_text_field($_POST['user_uuid']) : '';
if (!empty($new_uuid) && is_uuid($new_uuid)) {
update_user_meta($user_id, '_uuid', $new_uuid);
} else {
delete_user_meta($user_id, '_uuid');
}
}
function is_uuid($value)
{
$pattern = '/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i'; // UUID pattern.
return (bool) preg_match($pattern, $value);
}
add_action('show_user_profile', 'add_uuid_field_to_profile');
add_action('edit_user_profile', 'add_uuid_field_to_profile');
function add_uuid_field_to_profile($user)
{
$uuid = get_user_meta($user->ID, '_uuid', true);
?>
<table class="form-table">
<tr>
<th><label for="user_uuid"><?php esc_html_e('UUID', 'hongkiat'); ?></label></th>
<td>
<input
type="text"
name="user_uuid"
id="user_uuid"
value="<?php echo esc_attr($uuid); ?>"
class="regular-text"
<?php echo !current_user_can('manage_options') ? 'readonly' : ''; ?>
/>
<p class="description">
<?php
if (current_user_can('manage_options')) {
esc_html_e('Enter or update the UUID for this user.', 'hongkiat');
} else {
esc_html_e('This UUID is read-only for non-administrators.', 'hongkiat');
}
?>
</p>
</td>
</tr>
</table>
<?php
}
add_action('personal_options_update', 'save_uuid_field');
add_action('edit_user_profile_update', 'save_uuid_field');
function save_uuid_field($user_id)
{
if (!current_user_can('manage_options', $user_id)) {
return false;
}
$new_uuid = isset($_POST['user_uuid']) ? sanitize_text_field($_POST['user_uuid']) : '';
if (!empty($new_uuid) && is_uuid($new_uuid)) {
update_user_meta($user_id, '_uuid', $new_uuid);
} else {
delete_user_meta($user_id, '_uuid');
}
}
function is_uuid($value)
{
$pattern = '/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i'; // UUID pattern.
return (bool) preg_match($pattern, $value);
}
出于安全考虑,只有拥有 manage_options
权限的用户才能激活和编辑此输入,因此只有管理员才能为用户添加或更新 UUID。没有适当权限的用户只能看到只读输入。
更改作者页面URL
接下来,我们需要修改作者页面URL,使用 UUID 代替作者的用户名。如下所示,我们可以通过执行 author_link
过滤器来实现这一点:
add_filter('author_link', 'change_author_url', 10, 3);
function change_author_url($link, $author_id, $author_nicename)
$uuid = get_user_meta($author_id, '_uuid', true);
return str_replace('/' . $authorSlug, '/' . $uuid, $link);
add_filter('author_link', 'change_author_url', 10, 3);
function change_author_url($link, $author_id, $author_nicename)
{
$uuid = get_user_meta($author_id, '_uuid', true);
if (is_string($uuid)) {
return str_replace('/' . $authorSlug, '/' . $uuid, $link);
}
return $link;
}
add_filter('author_link', 'change_author_url', 10, 3);
function change_author_url($link, $author_id, $author_nicename)
{
$uuid = get_user_meta($author_id, '_uuid', true);
if (is_string($uuid)) {
return str_replace('/' . $authorSlug, '/' . $uuid, $link);
}
return $link;
}
该功能将更新为作者生成的 URL,同时影响前端主题和管理界面。

处理作者归档的查询
由于我们修改了作者归档 URL 的 URL 结构,因此还需要处理相应的查询。如果不这样做,WordPress 就会返回 404 Not Found错误,因为它无法识别如何通过作者的 _uuid
元数据来查询作者。
为了实现这一功能,我们可以使用 pre_get_posts
钩子,如下所示:
add_action('pre_get_posts', 'author_uuid_query');
function author_uuid_query($query) {
* If the permalink structure is set to plain, the author should be queried
if ((bool) get_option('permalink_structure') === false) {
$author_name = $query->query_vars['author_name'] ?? '';
if (! is_string($author_name) || ! is_uuid($author_name)) {
$query->is_author = false;
$query->is_archive = false;
'meta_value' => $author_name,
if (count($users) <= 0) {
$query->is_author = false;
$query->is_archive = false;
if (! $user instanceof WP_User) {
$query->is_author = false;
$query->is_archive = false;
$query->set('author_name', $user->user_nicename);
add_action('pre_get_posts', 'author_uuid_query');
function author_uuid_query($query) {
/**
* If the permalink structure is set to plain, the author should be queried
* by the user ID.
*/
if ((bool) get_option('permalink_structure') === false) {
return;
}
$author_name = $query->query_vars['author_name'] ?? '';
if (! is_string($author_name) || ! is_uuid($author_name)) {
$query->is_404 = true;
$query->is_author = false;
$query->is_archive = false;
return;
}
$users = get_users([
'meta_key' => '_uuid',
'meta_value' => $author_name,
]);
if (count($users) <= 0) {
$query->is_404 = true;
$query->is_author = false;
$query->is_archive = false;
return;
}
$user = $users[0];
if (! $user instanceof WP_User) {
$query->is_404 = true;
$query->is_author = false;
$query->is_archive = false;
return;
}
$query->set('author_name', $user->user_nicename);
}
add_action('pre_get_posts', 'author_uuid_query');
function author_uuid_query($query) {
/**
* If the permalink structure is set to plain, the author should be queried
* by the user ID.
*/
if ((bool) get_option('permalink_structure') === false) {
return;
}
$author_name = $query->query_vars['author_name'] ?? '';
if (! is_string($author_name) || ! is_uuid($author_name)) {
$query->is_404 = true;
$query->is_author = false;
$query->is_archive = false;
return;
}
$users = get_users([
'meta_key' => '_uuid',
'meta_value' => $author_name,
]);
if (count($users) <= 0) {
$query->is_404 = true;
$query->is_author = false;
$query->is_archive = false;
return;
}
$user = $users[0];
if (! $user instanceof WP_User) {
$query->is_404 = true;
$query->is_author = false;
$query->is_archive = false;
return;
}
$query->set('author_name', $user->user_nicename);
}
上面的代码会验证固定链接结构是否设置为默认的“Plain”设置以外的其他设置。我们不处理“Plain”链接结构的查询,因为在这种情况下,WordPress 使用的是作者 ID ( ?author=<id>
) 而不是 author_name
。
在REST API中更改作者标签
用户的用户名也在 /wp-json/wp/v2/users
REST API 端点中公开。为提高安全性,我们将用 UUID 替换用户名,从而对其进行修改。如下所示,我们可以通过实施 rest_prepare_user
钩子来实现这一点:
add_filter('rest_prepare_user', 'change_user_slug_in_rest_api', 10, 2);
function change_user_slug_in_rest_api($response, $user)
$data = $response->get_data();
$uuid = get_user_meta($author_id, '_uuid', true);
$response->set_data($data);
add_filter('rest_prepare_user', 'change_user_slug_in_rest_api', 10, 2);
function change_user_slug_in_rest_api($response, $user)
{
$data = $response->get_data();
if (is_array($data)) {
$uuid = get_user_meta($author_id, '_uuid', true);
if (is_string($uuid)) {
$data['slug'] = $uuid;
}
}
$response->set_data($data);
return $response;
}
add_filter('rest_prepare_user', 'change_user_slug_in_rest_api', 10, 2);
function change_user_slug_in_rest_api($response, $user)
{
$data = $response->get_data();
if (is_array($data)) {
$uuid = get_user_meta($author_id, '_uuid', true);
if (is_string($uuid)) {
$data['slug'] = $uuid;
}
}
$response->set_data($data);
return $response;
}
有了这种实现方式,作者 URL 现在将使用 UUID 而不是用户名。任何使用原始用户名访问作者 URL 的尝试都会导致 404 未找到错误。
虽然这种解决方案对小型网站或用户数量有限的网站很有效,但在处理大量用户时,管理起来就会变得很麻烦。在这种情况下,为每个用户手动设置 UUID 既费时又不切实际。
因此,让我们来探讨一种可提供更精简解决方案的替代方法。
简易法(安装插件)
为了获得更简单的解决方案,我们将使用一个名为 Feature Flipper 的插件。该插件提供多种安全功能,包括使用 UUID 混淆用户名。
你可以直接从 WordPress 仪表板的插件部分安装该插件。安装并激活后,导航至 “设置”>“Feature”>“Security”,然后启用“Obfuscate Usernames”选项。

保存设置后,插件将自动为网站上的所有现有用户生成 UUID。此外,它还会在新用户注册时为其分配 UUID。
小结
为作者页面 URL 实施 UUID 是一种有效的安全措施,可以通过隐藏作者用户名来保护 WordPress 网站。这种方法大大降低了暴力攻击和未经授权访问尝试的风险。
在本教程中,我们探讨了两种实施方法。对于喜欢自定义解决方案的用户,可以查看本次教程涉及的完整的源代码(GitHub 代码库) 。另外,Feature Flipper 插件为寻求现成解决方案的用户提供了一种更直接的方法。
评论留言