iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article

Automating WordPress Drafts and SNS Posts with a Single /blog Command

に公開

How to Automatically Generate WordPress Drafts and SNS Posts with a Single /blog Command

I collect and share IT trends every day, but doing the blog posting and creating SNS posts manually every time is honestly exhausting.

So, I used the slash command feature of Claude Code to create a system that automates WordPress draft posting and SNS text generation simply by passing it a Markdown file.

In this article, I will share the entire mechanism.


Overall Flow

By simply running /blog 2026-05-03.md, everything below is done automatically:

Read the Markdown file

Automatically generate the article title (Date filename → "IT Trend Summary for YYYY/MM/DD")

Extract keywords from the article content → Search/fetch images using the Pixabay API

Upload to the WordPress media library

Post as a draft via the WordPress REST API (with a featured image)

Automatically generate SNS posts and save to sns-draft.md
  ├── X (Japanese/English) within 269 characters each
  └── LinkedIn (Japanese/English)

The actual posting to SNS is manual, but by automating the generation of the posts and saving them as drafts, the only thing left for me to do is copy and paste.


Implementation Details

1. Reading the Markdown File and Generating the Title

If the filename is in a date format like 2026-05-03.md, it automatically converts the title to IT Trend Summary for 2026/05/03. Zero-padding is preserved.

If it is not in a date format, it uses the first H1 heading in the Markdown as the title.

I extract keywords from the article content in English and search for images using the Pixabay API.

Endpoint:
https://pixabay.com/api/?key={API_KEY}&q={KEYWORD}&image_type=photo&orientation=horizontal&per_page=5&lang=en

The fetched image is downloaded to /tmp/ and then uploaded to the WordPress media library using the WordPress REST API.

Pixabay is free to use. You can get an API key from the API page after registering an account at Pixabay.

3. Automatic Draft Posting via the WordPress REST API

I use the REST API to post to WordPress. Authentication is handled via an Application Password.

How to set up an Application Password:

  1. WordPress Dashboard → Users → Profile
  2. Go to the "Application Passwords" section at the bottom of the screen
  3. Enter an application name (e.g., api-poster) and click "Add New Application Password"
  4. Copy and save the displayed password (it will not be shown again)

The post status is set to draft. For automatic posting, there is a risk that unintended content could be published as-is, so I have designed it so that a human performs the final review.

4. Automatic Generation of SNS Posts

It picks up 3-5 main topics from the article and generates posts in the format for each SNS. The generated content is saved in sns-draft.md in the current directory.

Handling X's Character Limit:

Under X's URL shortening specification, URLs are counted as 23 characters regardless of their actual length. I have explicitly stated this specification in the prompt and included an instruction for Claude to count the characters itself after generation and modify the post if it exceeds 269 characters.


Running It in Practice

Here is an example of the output when running /blog 2026-05-03.md.

Completion Report from Claude Code:

Post title: IT Trend Summary for 2026/05/03
Post ID: 1637
Draft URL: https://obataka.com/?p=1637
Featured image: https://pixabay.com/get/xxxxx.jpg

Saved to sns-draft.md.

Content of the generated sns-draft.md (X Japanese version):

IT Trend Summary for 2026/05/03

・Google Deep Research Max: Gemini 3.1 Pro enables cross-company data search with MCP integration
・SWE-bench 2026: Claude Mythos Preview achieves a record 93.9%
・LangGraph 1.0 GA: Becomes a production-ready agent with durable state and HiTL support

Read more here👇
https://obataka.com/?p=1637

The character count is 205, which is well within the 269-character limit.


Security Best Practice: Create a Dedicated User for WordPress Posting

When posting via the WordPress REST API, using an administrator account (like 'admin') carries risks. If the API key or authentication credentials were ever leaked, there is a possibility that the entire site could be compromised.

Therefore, I created a separate user, api-poster, dedicated to posting, and issued an Application Password for that specific user.

Steps to create a dedicated user:

  1. WordPress Dashboard → Users → Add New
  2. Set the username to something descriptive like api-poster
  3. Set the role to "Author" (administrative privileges are unnecessary)
  4. Generate an Application Password on the profile screen of the newly created user

By restricting permissions to "Author," even if the credentials were leaked, the potential impact is limited solely to posting articles. Since authentication credentials are included in the prompt file, I believe this isolation is a mandatory minimum precaution.


Troubleshooting: The X Character Count Issue

My initial prompt did not include specific instructions regarding character counts.

When I first attempted to post, I noticed the generated text exceeded the limit. I had to repeatedly correct it by telling Claude, "You are X characters over," and having it try again. The current prompt is the result of that trial and error.

Specifically, I have explicitly included the following two points in the prompt:

  • Count URLs as 23 characters (X's URL shortening specification)
  • After drafting the text, count the characters yourself to verify, and if it exceeds the limit, iteratively refine it until it is short enough

I have been using this workflow for over a month since adding these instructions, and character limit overflows have not occurred once.


Full Prompt for the /blog Command

For reference, here is the structure of the prompt I currently use (authentication details like API keys and passwords are omitted).

---
description: Post a draft to WordPress from a Markdown file
---

Follow these steps to post a draft to WordPress.

## Procedure

### 1. File Reading and Title Generation
- Read the specified Markdown file
- If the filename is in YYYY-MM-DD.md format, convert the title to "IT Trend Summary for YYYY/MM/DD"
- If not in date format, use the first H1 heading in the Markdown as the title

### 2. Fetch Featured Image
- Brainstorm keywords appropriate for the article from the Markdown content in English
- Search for images via Pixabay API
- Download the most appropriate image and upload it to the WordPress media library

### 3. WordPress Draft Posting
- Post as a draft using the generated title and the retrieved media ID
- Status: draft

### 4. Completion Report
- Report the post title, post ID, draft URL, and featured image URL

### 5. Save SNS Text to File
- Pick up 3-5 main topics and save them to sns-draft.md

[X (Twitter) - Japanese/English, within 269 characters each]
- Count URLs as 23 characters
- Count characters yourself and correct if it exceeds the limit

[LinkedIn - Japanese/English]
- Summary (3-4 sentences) + 5 main topics + Hashtags

Conclusion

With this single command, the work from writing Markdown to completing a WordPress draft has become almost zero.

All that remains is for me to check the content in the WordPress dashboard and hit the publish button, and copy-paste the contents of sns-draft.md for social media.

I use this /blog command in combination with the /trend command introduced in another article. By collecting IT trends to generate Markdown with /trend and posting that Markdown to WordPress with /blog, I have achieved end-to-end automation from trend collection to posting.

I explain the mechanism of the /trend command in the article below.

Introduction to an AI-powered technical information collection and dissemination workflow

Discussion