高级管理文档

💡 云策文档标注

概述

应用密码是 WordPress 的一项功能,允许为程序化访问(如移动应用、集成或脚本)生成可撤销的、针对特定应用的凭证,旨在避免与第三方工具共享主账户密码。该功能自 WordPress 5.6(2020年12月)引入,适用于 API 认证,而非交互式浏览器登录。

关键要点

  • 应用密码与特定 WordPress 用户账户绑定,用于 REST API 或 XML-RPC 认证,不能用于 wp-admin 登录。
  • 密码在创建时仅显示一次,以哈希形式存储,可单独撤销,不影响用户主密码。
  • 使用场景包括部署脚本、第三方服务集成或桌面/移动客户端访问 API。
  • 通过 wp-admin 用户配置文件创建和管理应用密码,建议使用描述性名称并立即复制存储。
  • 应用密码通常通过 HTTP Basic Authentication 使用,需 Base64 编码用户名和密码,并始终配合 HTTPS 以确保安全。
  • 最佳实践包括为每个集成创建独立密码、定期轮换和及时撤销不再需要的凭证。
  • WP-CLI 支持创建、列出、更新和撤销应用密码,便于脚本和 CI 环境管理。
  • 默认在 HTTPS 下可用,可通过过滤器自定义或禁用,例如使用 wp_is_application_passwords_available 过滤器。
  • 故障排除涉及检查 HTTPS 配置、确认使用应用密码而非主密码、验证功能是否被禁用,以及确保客户端正确发送 Basic Auth 头。

代码示例

# 使用 curl 通过应用密码进行 REST API 请求示例
curl --user "USERNAME:APPLICATION_PASSWORD" https://example.com/wp-json/wp/v2/users/me

# WP-CLI 命令示例
# 为用户 ID 123 创建应用密码
wp user application-password create 123 "myapp"
# 仅输出密码(适用于脚本)
wp user application-password create 123 "myapp" --porcelain
# 列出用户 ID 123 的应用密码
wp user application-password list 123 --fields=uuid,name,created,last_used,last_ip
# 删除特定应用密码
wp user application-password delete 123 <uuid>

注意事项

  • 应用密码不能用于 wp-login.php 的交互式登录,仅适用于程序化访问。
  • 必须使用 HTTPS 以防止凭证在网络上被拦截,确保站点正确配置 TLS。
  • 创建后立即复制密码,因为它不会再次显示,建议作为机密信息处理。
  • 如果功能不可用,检查是否有安全插件或自定义代码通过过滤器禁用了它。

📄 原文内容

Application Passwords are a WordPress feature that lets you generate revocable, per-application credentials for programmatic access (for example, a mobile app, an integration, or a script). They are designed to avoid sharing your main account password with third-party tools.

Application Passwords were introduced in WordPress 5.6 (December 2020). See: Application Passwords: Integration Guide.

What are Application Passwords?

An Application Password is:

  • A password tied to a specific WordPress user account.
  • Intended for API authentication (for example, WordPress REST API, and (where enabled) XML-RPC), not for interactive browser logins.
  • Stored hashed in WordPress and shown only once at creation time.
  • Individually revocable, so you can disable a single integration without changing the user’s primary password.

Application Passwords cannot be used to log into wp-admin via wp-login.php. They are meant for applications and scripts, not humans typing credentials into a login form.

When to use Application Passwords

Use Application Passwords when you need a tool to authenticate as a WordPress user without giving it that user’s main password, for example:

  • A deployment/maintenance script that needs to call the REST API.
  • A third-party service that posts content or uploads media to WordPress.
  • A desktop/mobile client that needs authenticated access to your site’s APIs.

If the use-case is interactive logins in a browser, use a strong password and consider enabling Two-Step Authentication (also known as two-factor authentication, or 2FA): Two-Step Authentication.

Creating an Application Password in wp-admin

  1. Log into wp-admin as the user who needs the credential (or an admin editing that user).
  2. Go to Users Profile (or Users All Users Edit for another user).
  3. Find the Application Passwords section.
  4. Enter a descriptive name (for example: CI deploy bot, My iPhone app, Reporting integration).
  5. Generate the password and copy/store it immediately (it will not be shown again).

WordPress displays Application Passwords in grouped chunks for readability. They can be used with or without the spaces (spaces are stripped before validation).

Using an Application Password

Application Passwords are typically used via HTTP Basic Authentication (RFC 7617). With Basic Auth, your client sends credentials in an Authorization HTTP header.

The header value is constructed by Base64-encoding the string username:password:

Authorization: Basic BASE64_ENCODED_CREDENTIALS

When using Application Passwords:

  • Username: the WordPress username (login).
  • Password: the generated Application Password.

Because Basic Auth credentials can be intercepted on the network if sent unencrypted, you should always use HTTPS. See: HTTPS.

Example (REST API request with curl):

curl --user "USERNAME:APPLICATION_PASSWORD" https://example.com/wp-json/wp/v2/users/me

Managing and rotating Application Passwords

In the same Application Passwords section on the user profile, you can:

  • Review existing Application Passwords (by name).
  • See usage metadata (for example, last used time / last IP).
  • Revoke individual passwords.

Operational best practice is to treat Application Passwords like secrets:

  • Create one per integration/app (don’t reuse across tools).
  • Revoke any credential that is no longer needed.
  • Rotate credentials on a schedule or after any suspected leak.

Managing Application Passwords with WP-CLI

If you manage WordPress via SSH, WP-CLI can create, list, update, and revoke Application Passwords.

Examples:

# Create an application password for user ID 123 (prints the password once).
wp user application-password create 123 "myapp"

# Create and output just the password for user ID 123 (useful for scripts/CI).
wp user application-password create 123 "myapp" --porcelain

# List passwords for user ID 123 and show common fields.
wp user application-password list 123 --fields=uuid,name,created,last_used,last_ip

# Delete a specific application password by UUID for user ID 123.
wp user application-password delete 123 <uuid>

Official command reference: wp user application-password.

Availability and disabling

By default, Application Passwords are available when requests are served over HTTPS. Availability can be customized (or disabled) with filters.

If you do not want to allow Application Passwords on a site, you can disable them via code (for example, in a must-use plugin) using the wp_is_application_passwords_available filter. For more granular control (for example, only allow specific users/roles), use wp_is_application_passwords_available_for_user.

Troubleshooting

If authentication fails (401/403) or Application Passwords don’t appear in the user profile:

  • Confirm HTTPS: Application Passwords are intended for HTTPS requests. Ensure the site is correctly configured for TLS and that WordPress detects it as HTTPS. See: HTTPS.
  • Confirm you’re using an Application Password: The user’s regular password may still be required for interactive logins; the Application Password is separate.
  • Check whether the feature is disabled: Security plugins, must-use plugins, or custom code may disable Application Passwords via filters.
  • Confirm your client sends Basic Auth: Some HTTP clients and proxies can strip Authorization headers unless explicitly configured.