WordPress API
Interact with WordPress sites via the REST API. Create, update, and manage posts, pages, media, and more on any WordPress installation.
Quick Start
Single Site (Direct)
CODEBLOCK0
Multi-Site (CLI Wrapper) ⭐
CODEBLOCK1
Authentication
WordPress REST API supports two authentication methods:
1. Application Password (Recommended) ⭐
Setup:
- 1. Go to: INLINECODE0
- Scroll to "Application Passwords"
- Name: "OpenClaw API" (or any name)
- Click "Add New Application Password"
- Copy the password (shows only once!)
Format: xxxx xxxx xxxx xxxx xxxx xxxx (24 characters with spaces)
Usage: Pass as --app-password to scripts or set WP_APP_PASSWORD environment variable
2. Basic Auth (Legacy)
Requires Basic Auth plugin. Less secure, not recommended for production.
Environment Variables
Set these to avoid passing credentials every time:
CODEBLOCK2
Multi-Site Management
Configuration
- 1. Copy the example config:
CODEBLOCK3
- 2. Edit
config/sites.json with your sites:
CODEBLOCK4
CLI Wrapper (./wp.sh)
CODEBLOCK5
Batch Operations
CODEBLOCK6
Individual Scripts
All scripts support both command-line arguments and environment variables.
Update Post
CODEBLOCK7
Supports:
- - Content (HTML or Gutenberg blocks)
- Title
- Status (publish, draft, pending, private)
- Featured image ID
- Custom fields (meta)
Create Post
CODEBLOCK8
Get Post
CODEBLOCK9
Returns full post data as JSON.
List Posts
CODEBLOCK10
Options:
- -
--per-page N - Posts per page (default: 10, max: 100) - INLINECODE7 - Page number
- INLINECODE8 - Filter by status (publish, draft, pending, private)
- INLINECODE9 - Filter by author
Plugin Integration
Detect Plugins
Automatically detect installed WordPress plugins (ACF, Rank Math, Yoast, JetEngine):
CODEBLOCK11
ACF (Advanced Custom Fields)
Read and write ACF fields with automatic fallback to postmeta:
CODEBLOCK12
Features:
- - Automatic REST API detection with postmeta fallback
- Support for simple, repeater, and group fields
- JSON input/output for complex field structures
SEO Meta (Rank Math & Yoast)
Read and write SEO meta fields with auto-detection:
CODEBLOCK13
Supported fields:
- - Rank Math: title, description, focuskeyword, robots, canonicalurl, schema
- Yoast: title, description, focuskeyword, canonical, metarobotsnoindex, metarobots_nofollow
JetEngine Fields
Read and write JetEngine custom fields (stored as postmeta):
CODEBLOCK14
Elementor Content
Manage Elementor page builder content via _elementor_data meta field:
CODEBLOCK15
Supported widget types:
- - Headings (
heading) - Text Editor (
text-editor) - Buttons (
button) - Generic fallback for other widgets
How it works:
- - Gets page data with
_elementor_data meta field - Parses JSON structure
- Finds widget by ID (recursively through nested elements)
- Updates content based on widget type
- Saves back to WordPress
Media Upload
Upload images and files to WordPress media library:
CODEBLOCK16
Features:
- - Supports local files and URLs
- Auto-detects MIME type
- Returns media ID for further operations
- Can set as featured image in one call
WooCommerce Products
Manage WooCommerce products via REST API v3:
CODEBLOCK17
Authentication:
- - Requires WooCommerce Consumer Key and Consumer Secret
- Get from: WooCommerce → Settings → Advanced → REST API
- Environment variables:
WC_CONSUMER_KEY, INLINECODE16
Supported fields:
- -
--title / --name - Product name - INLINECODE19 - Regular price
- INLINECODE20 - Sale price
- INLINECODE21 - Full description
- INLINECODE22 - Short description
- INLINECODE23 - publish, draft, pending, private
- INLINECODE24 - Stock keeping unit
- INLINECODE25 - Stock amount
- INLINECODE26 - Enable stock management
Gutenberg Blocks
WordPress uses Gutenberg block format for content. See references/gutenberg-blocks.md for details.
Example block:
CODEBLOCK18
API Reference
See references/api-reference.md for complete WordPress REST API documentation.
Common endpoints:
- -
/wp-json/wp/v2/posts - Posts - INLINECODE28 - Pages
- INLINECODE29 - Media
- INLINECODE30 - Users
- INLINECODE31 - Categories
- INLINECODE32 - Tags
Error Handling
All scripts return:
- - Exit code 0 on success
- Exit code 1 on error
- JSON output to stdout
- Error messages to stderr
Security
- - ✅ DO: Use Application Passwords
- ✅ DO: Use HTTPS only
- ✅ DO: Store credentials in environment variables
- ❌ DON'T: Hardcode passwords in scripts
- ❌ DON'T: Use Basic Auth in production
- ❌ DON'T: Commit credentials to git
Limitations
- - Requires WordPress 4.7+ (REST API built-in)
- Requires Application Passwords plugin for WordPress <5.6
- Some endpoints require specific user permissions
- Rate limiting depends on host configuration
WordPress API
通过REST API与WordPress站点交互。在任何WordPress安装上创建、更新和管理文章、页面、媒体等。
快速开始
单站点(直接)
bash
更新一篇文章
python3 scripts/update_post.py \
--url https://example.com \
--username admin \
--app-password xxxx xxxx xxxx xxxx \
--post-id 123 \
--content 此处为新内容 \
--status publish
多站点(CLI封装器)⭐
bash
设置:复制配置模板并添加你的站点
cp config/sites.example.json config/sites.json
编辑 config/sites.json 添加你的站点
列出已配置的站点
./wp.sh --list-sites
在特定站点上更新文章
./wp.sh digitizer-studio update-post --id 123 --content 新内容
在组内所有站点上运行
./wp.sh digitizer update-post --id 456 --status publish
跨多个站点批量更新
python3 scripts/batch_update.py --group digitizer --post-ids 123,456 --status publish
身份验证
WordPress REST API支持两种身份验证方法:
1. 应用程序密码(推荐)⭐
设置方法:
- 1. 访问:https://yoursite.com/wp-admin/profile.php
- 滚动至应用程序密码
- 名称:OpenClaw API(或任意名称)
- 点击添加新应用程序密码
- 复制密码(仅显示一次!)
格式: xxxx xxxx xxxx xxxx xxxx xxxx(24个字符,含空格)
用法: 作为 --app-password 参数传递给脚本,或设置 WPAPPPASSWORD 环境变量
2. 基本认证(旧版)
需要Basic Auth插件。安全性较低,不推荐用于生产环境。
环境变量
设置以下变量可避免每次传递凭据:
bash
export WP_URL=https://example.com
export WP_USERNAME=admin
export WPAPPPASSWORD=xxxx xxxx xxxx xxxx xxxx xxxx
多站点管理
配置
- 1. 复制示例配置:
bash
cp config/sites.example.json config/sites.json
- 2. 编辑 config/sites.json 添加你的站点:
json
{
sites: {
my-site: {
url: https://mysite.com,
username: admin,
app_password: xxxx xxxx xxxx,
description: 我的WordPress站点
}
},
groups: {
all: [my-site]
}
}
CLI封装器(./wp.sh)
bash
列出站点
./wp.sh --list-sites
./wp.sh --list-groups
单站点操作
./wp.sh my-site update-post --id 123 --content ...
./wp.sh my-site create-post --title ... --content ...
./wp.sh my-site get-post --id 123
./wp.sh my-site list-posts --per-page 10
组操作(在组内所有站点上运行)
./wp.sh all update-post --id 123 --status publish
批量操作
bash
跨多个站点更新多篇文章
python3 scripts/batch_update.py \
--group digitizer \
--post-ids 123,456,789 \
--status publish
在所有站点上更新元字段
python3 scripts/batch_update.py \
--sites all \
--post-ids 100 \
--meta-key seo_score \
--meta-value 95
试运行(查看将要执行的操作)
python3 scripts/batch_update.py \
--group all \
--post-ids 123 \
--status draft \
--dry-run
独立脚本
所有脚本均支持命令行参数和环境变量。
更新文章
bash
python3 scripts/update_post.py \
--post-id 123 \
--content 已更新的内容 \
--title 新标题 \
--status publish
支持:
- - 内容(HTML或Gutenberg区块)
- 标题
- 状态(publish、draft、pending、private)
- 特色图片ID
- 自定义字段(meta)
创建文章
bash
python3 scripts/create_post.py \
--title 新文章 \
--content 文章内容 \
--status draft
获取文章
bash
python3 scripts/get_post.py --post-id 123
以JSON格式返回完整文章数据。
列出文章
bash
python3 scripts/list_posts.py --per-page 10 --status publish
选项:
- - --per-page N - 每页文章数(默认:10,最大:100)
- --page N - 页码
- --status STATUS - 按状态筛选(publish、draft、pending、private)
- --author ID - 按作者筛选
插件集成
检测插件
自动检测已安装的WordPress插件(ACF、Rank Math、Yoast、JetEngine):
bash
python3 scripts/detect_plugins.py
python3 scripts/detect_plugins.py --verbose
ACF(高级自定义字段)
读写ACF字段,自动回退到postmeta:
bash
获取所有ACF字段
python3 scripts/acf_fields.py --post-id 123
获取特定字段
python3 scripts/acf
fields.py --post-id 123 --field myfield
从JSON设置字段
python3 scripts/acf_fields.py --post-id 123 --set {field1: value1, field2: value2}
设置单个字段
python3 scripts/acf
fields.py --post-id 123 --field myfield --value my value
特性:
- - 自动REST API检测,带postmeta回退
- 支持简单字段、重复器字段和组字段
- 复杂字段结构的JSON输入/输出
SEO元数据(Rank Math和Yoast)
读写SEO元字段,自动检测:
bash
自动检测插件并获取元数据
python3 scripts/seo_meta.py --post-id 123
检测哪个SEO插件处于活动状态
python3 scripts/seo_meta.py --post-id 123 --detect
获取特定插件的元数据
python3 scripts/seo_meta.py --post-id 123 --plugin rankmath
python3 scripts/seo_meta.py --post-id 123 --plugin yoast
设置SEO元数据
python3 scripts/seo_meta.py --post-id 123 --set {title: SEO标题, description: 元描述}
使用特定插件设置
python3 scripts/seo
meta.py --post-id 123 --plugin rankmath --set {focuskeyword: 关键词}
支持的字段:
- - Rank Math: title、description、focuskeyword、robots、canonicalurl、schema
- Yoast: title、description、focuskeyword、canonical、metarobotsnoindex、metarobots_nofollow
JetEngine字段
读写JetEngine自定义字段(存储为postmeta):
bash
获取所有JetEngine字段
python3 scripts/jetengine_fields.py --post-id 123
获取特定字段
python3 scripts/jetengine
fields.py --post-id 123 --field myfield
从JSON设置字段
python3 scripts/jetengine_fields.py --post-id 123 --set {field1: value1, field2: value2}
设置单个字段
python3 scripts/jetengine
fields.py --post-id 123 --field myfield --value my value
Elementor内容
通过 elementordata 元字段管理Elementor页面构建器内容:
bash
获取页面的Elementor数据
python3 scripts/elementor_content.py --post-id 123 --action get
更新特定小工具内容
python3 scripts/elementor_content.py \
--post-id 123 \
--action update \
--widget-id abc123 \
--content 新标题文本
支持的小工具类型:
- - 标题(heading)
- 文本编辑器(text-editor)
- 按钮(button)
- 其他小工具的通用回退
工作原理:
- - 通过 elementordata 元字段获取页面数据
- 解析JSON结构
- 按ID查找小工具(递归遍历嵌套元素)
- 根据小工具类型更新内容
- 保存回WordPress
媒体上传
上传图片和文件到WordPress媒体库:
bash