← 返回
未分类 Key 中文

Blog with Wordpress

Publish articles to WordPress blogs via REST API. Handles post creation, category/tag management, and SEO-friendly English slug generation. Use when user ask...
通过 REST API 将文章发布到 WordPress 博客,负责文章创建、分类/标签管理以及 SEO 友好的英文别名生成,适用于用户请求发布文章时。
hugogu
未分类 clawhub v1.0.0 1 版本 100000 Key: 需要
★ 1
Stars
📥 343
下载
💾 0
安装
1
版本
#latest

概述

WordPress Blog Publisher

Publish articles to WordPress blogs safely with automatic category/tag management and English URL slugs.


Prerequisites

WordPress credentials must be configured in the workspace .env file:

# WordPress Blog Credentials
WP_BLOG_URL="https://blog.example.com"      # Blog base URL (no trailing slash)
WP_USERNAME="your_username"                  # WordPress admin username
WP_APP_PASSWORD="xxxx xxxx xxxx xxxx xxxx"   # Application password

How to create an Application Password:

  1. Log in to WordPress admin dashboard
  2. Go to Users → Profile
  3. Scroll to "Application Passwords" section
  4. Click "Add New Application Password"
  5. Copy the generated password

Step 1 — Read Credentials

Read credentials from workspace .env:

# Load credentials from .env file
source /root/.openclaw/workspace/.env

WP_URL="${WP_BLOG_URL:-https://blog.example.com}"
WP_USER="${WP_USERNAME:-admin}"
WP_PASS="${WP_APP_PASSWORD}"

# Verify credentials exist
if [ -z "$WP_PASS" ]; then
  echo "❌ Error: WP_APP_PASSWORD not found in .env file"
  exit 1
fi

Step 2 — Analyze Content & Generate Metadata

Before publishing, analyze the article content to generate appropriate metadata:

Generate English Slug

Create a URL-friendly English slug from the article title or content:

  • Use lowercase with hyphens as separators
  • Keep it under 50 characters when possible
  • Include main keywords
  • Remove stop words (a, an, the, and, or, etc.)

Examples:

  • "AMD Ryzen 9 7950X vs Intel Core i9-13900K: A Detailed Benchmark Comparison" → ryzen-7950x-vs-i9-13900k-benchmark-comparison
  • "How to Optimize Database Performance in Production" → optimize-database-performance-production
  • "Understanding Container Orchestration with Kubernetes" → understanding-container-orchestration-kubernetes

Suggest Categories & Tags

Based on article content, suggest appropriate WordPress categories and tags:

Content TypeSuggested CategoriesSuggested Tags
--------------------------------------------------
Hardware reviewsHardware, ReviewsCPU, benchmark, performance, AMD, Intel
Software developmentDevelopment, Programmingcoding, best-practices, architecture
AI/LLM relatedAI, Technologymachine-learning, LLM, artificial-intelligence
Career developmentCareercareer-growth, soft-skills, productivity
DevOps/InfrastructureDevOps, Infrastructuredocker, kubernetes, ci-cd, cloud

If user doesn't specify, use these reasonable defaults:

  • Category: Based on content topic (create if not exists)
  • Tags: Extract 2-4 keywords from content

Step 3 — Create Category (if needed)

Check if category exists, create if not:

CATEGORY_NAME="Hardware"  # Use suggested or user-specified category

# Try to find existing category
CAT_ID=$(curl -s "${WP_URL}/wp-json/wp/v2/categories?search=${CATEGORY_NAME}&per_page=1" \
  -u "${WP_USER}:${WP_PASS}" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)

# Create if not exists
if [ -z "$CAT_ID" ]; then
  CAT_RESULT=$(curl -s -X POST "${WP_URL}/wp-json/wp/v2/categories" \
    -u "${WP_USER}:${WP_PASS}" \
    -H "Content-Type: application/json" \
    -d "{\"name\": \"${CATEGORY_NAME}\"}")
  CAT_ID=$(echo "$CAT_RESULT" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
fi

echo "Category ID: $CAT_ID"

Step 4 — Create Tags (if needed)

For each tag, check existence and create if needed:

TAGS=("CPU" "Benchmark" "AMD" "Performance")  # Use suggested or user-specified tags
TAG_IDS=""

for TAG in "${TAGS[@]}"; do
  # Try to find existing tag
  TID=$(curl -s "${WP_URL}/wp-json/wp/v2/tags?search=${TAG}&per_page=1" \
    -u "${WP_USER}:${WP_PASS}" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
  
  # Create if not exists
  if [ -z "$TID" ]; then
    TAG_RESULT=$(curl -s -X POST "${WP_URL}/wp-json/wp/v2/tags" \
      -u "${WP_USER}:${WP_PASS}" \
      -H "Content-Type: application/json" \
      -d "{\"name\": \"${TAG}\"}")
    TID=$(echo "$TAG_RESULT" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
  fi
  
  TAG_IDS="${TAG_IDS},${TID}"
done

# Remove leading comma
TAG_IDS=$(echo "$TAG_IDS" | sed 's/^,//')
echo "Tag IDs: $TAG_IDS"

Step 5 — Create or Update Post

Create New Post

TITLE="AMD Ryzen 9 7950X vs Intel Core i9-13900K: A Detailed Benchmark Comparison"
CONTENT="<p>In this comprehensive benchmark analysis...</p>"  # Convert markdown to HTML
SLUG="ryzen-7950x-vs-i9-13900k-benchmark-comparison"
EXCERPT="We compare two flagship processors across gaming, productivity, and power efficiency."

# Create post
POST_RESULT=$(curl -s -X POST "${WP_URL}/wp-json/wp/v2/posts" \
  -u "${WP_USER}:${WP_PASS}" \
  -H "Content-Type: application/json" \
  -d "{
    \"title\": \"${TITLE}\",
    \"content\": \"${CONTENT}\",
    \"slug\": \"${SLUG}\",
    \"status\": \"publish\",
    \"categories\": [${CAT_ID}],
    \"tags\": [${TAG_IDS}],
    \"excerpt\": \"${EXCERPT}\"
  }")

POST_ID=$(echo "$POST_RESULT" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
echo "Created post ID: $POST_ID"

Update Existing Post

If updating an existing post (e.g., adding categories/tags to a draft):

POST_ID="123"  # Existing post ID

UPDATE_RESULT=$(curl -s -X POST "${WP_URL}/wp-json/wp/v2/posts/${POST_ID}" \
  -u "${WP_USER}:${WP_PASS}" \
  -H "Content-Type: application/json" \
  -d "{
    \"categories\": [${CAT_ID}],
    \"tags\": [${TAG_IDS}],
    \"slug\": \"${SLUG}\"
  }")

Step 6 — Generate Public URL

Construct the public viewing URL (not the API endpoint):

# WordPress permalink structure: /{slug}/
PUBLIC_URL="${WP_URL}/${SLUG}/"

# If slug not set, use post ID format
if [ -z "$SLUG" ]; then
  PUBLIC_URL="${WP_URL}/?p=${POST_ID}"
fi

echo "✅ Article published successfully!"
echo ""
echo "📄 Title: ${TITLE}"
echo "🔗 URL: ${PUBLIC_URL}"
echo "📁 Category: ${CATEGORY_NAME}"
echo "🏷️ Tags: ${TAGS[*]}"

Content Conversion

Markdown to HTML

WordPress content field requires HTML. Convert markdown:

MarkdownHTML
----------------
# Title

Title

## Subtitle

Subtitle

### H3

H3

boldbold
italicitalic
- list item
  • list item
1. item
  1. item
texttext
` code `code
```code block```
code block

Handling Special Characters

Escape double quotes in content when building JSON:

# Escape quotes for JSON
ESCAPED_CONTENT=$(echo "$CONTENT" | sed 's/"/\\"/g')

Complete Workflow Example

#!/bin/bash

# Load credentials
source /root/.openclaw/workspace/.env
WP_URL="${WP_BLOG_URL:-https://blog.example.com}"
WP_USER="${WP_USERNAME:-admin}"
WP_PASS="${WP_APP_PASSWORD}"

# Article content - CPU Benchmark example
TITLE="AMD Ryzen 9 7950X vs Intel Core i9-13900K: A Detailed Benchmark Comparison"
SLUG="ryzen-7950x-vs-i9-13900k-benchmark-comparison"
CATEGORY="Hardware"
TAGS=("CPU" "Benchmark" "AMD" "Intel" "Performance")

CONTENT="<p>The battle for desktop CPU supremacy continues...</p><h2>Test Methodology</h2><p>All tests were conducted on identical platforms...</p>"

# Step 1: Create/Get Category
CAT_RESULT=$(curl -s -X POST "${WP_URL}/wp-json/wp/v2/categories" \
  -u "${WP_USER}:${WP_PASS}" \
  -H "Content-Type: application/json" \
  -d "{\"name\": \"${CATEGORY}\"}")
CAT_ID=$(echo "$CAT_RESULT" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)

# Step 2: Create/Get Tags
TAG_IDS=""
for TAG in "${TAGS[@]}"; do
  TAG_RESULT=$(curl -s -X POST "${WP_URL}/wp-json/wp/v2/tags" \
    -u "${WP_USER}:${WP_PASS}" \
    -H "Content-Type: application/json" \
    -d "{\"name\": \"${TAG}\"}")
  TID=$(echo "$TAG_RESULT" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
  TAG_IDS="${TAG_IDS},${TID}"
done
TAG_IDS=$(echo "$TAG_IDS" | sed 's/^,//')

# Step 3: Create Post
POST_RESULT=$(curl -s -X POST "${WP_URL}/wp-json/wp/v2/posts" \
  -u "${WP_USER}:${WP_PASS}" \
  -H "Content-Type: application/json" \
  -d "{
    \"title\": \"${TITLE}\",
    \"content\": \"${CONTENT}\",
    \"slug\": \"${SLUG}\",
    \"status\": \"publish\",
    \"categories\": [${CAT_ID}],
    \"tags\": [${TAG_IDS}]
  }")

POST_ID=$(echo "$POST_RESULT" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
PUBLIC_URL="${WP_URL}/${SLUG}/"

echo "✅ Published: ${PUBLIC_URL}"

Error Handling

Common Errors

ErrorCauseSolution
------------------------
401 UnauthorizedInvalid credentialsCheck username and app password
403 ForbiddenInsufficient permissionsUse admin account or check user capabilities
rest_cannot_createMissing edit_posts capabilityVerify user has publishing permissions
term_existsCategory/tag already existsFetch existing ID instead of creating

API Response Check

Always check API responses for errors:

if echo "$RESULT" | grep -q '"code":"'; then
  ERROR_CODE=$(echo "$RESULT" | grep -o '"code":"[^"]*"' | head -1)
  ERROR_MSG=$(echo "$RESULT" | grep -o '"message":"[^"]*"' | head -1)
  echo "❌ API Error: $ERROR_CODE - $ERROR_MSG"
  exit 1
fi

Safety Rules

  • ✅ Always generate English slug for SEO-friendly URLs
  • ✅ Create reasonable category/tags if user doesn't specify
  • ✅ Return public viewing URL, not API endpoint
  • ✅ Escape content properly for JSON payload
  • ✅ Verify credentials before attempting API calls
  • ❌ Never hardcode credentials in scripts
  • ❌ Never return API URLs (with /wp-json/) as the result

Response Format

After successful publication, respond with:

✅ Article published successfully!

📄 Title: [Article Title]
🔗 URL: [Public Viewing URL]
📁 Category: [Category Name]
🏷️ Tags: [Tag List]

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-05-07 09:55 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

Publish Wiki Pages

hugogu
将 Markdown内容发布到 Wiki.js,保持格式和元数据。用于创建或更新 Wiki 页面,或将笔记/文章转换为 Wiki 格式。
★ 0 📥 360

Commit code safe and nice

hugogu
智能Git提交,支持远程同步、自动修正和常规提交格式。当用户请求提交、分阶段提交、执行“/commit”或保存时使用。
★ 0 📥 348

Query Payful Account

hugogu
查询Payful账户信息,包括余额、交易记录和账户详情。用于用户需要查询Payful账户状态或查看余额时。
★ 0 📥 337