You are currently viewing Automate WordPress Posting with Python

Automate WordPress Posting with Python

Automate WordPress Posting with Python – Step-by-Step Guide


Want to save time? Learn how to automate WordPress posting with Python using REST API, app passwords, and Python scripts to publish content automatically.

If you’re ready to save time and boost efficiency, learning how to automate tasks with Python is a game-changer. Our guide on easy automation methods shows you step-by-step ways to handle repetitive work effortlessly, helping you focus on what really matters. Read More…!


Automate WordPress Posting with Python – Step-by-Step Guide

Do you manage a WordPress blog and feel tired of posting manually every time? Whether you’re working with AI-generated content or managing bulk blog schedules, you’ll love this automation trick.

In this article, I’ll show you how to automate WordPress posting with Python using the official REST API. This method is perfect for bloggers, developers, or content managers who want to simplify and streamline content publishing.


🔥 Why Automate WordPress Blog Posts?

Let’s be honest — logging in, pasting content, setting the title, categories, tags, featured image, and hitting “publish” every single time… it adds up. Especially when:

  • You use ChatGPT or Notion AI to generate articles

  • You post daily/weekly in bulk

  • You want to publish remotely or with a single click

  • You’re setting up a content system for your team

Here’s what you gain with automation:

✅ Save time
✅ Boost productivity
✅ Ensure consistency
✅ Enable integrations with AI, Google Sheets, or CSV inputs
✅ Run your content pipeline like a pro!


🧰 What You Need to Automate WordPress with Python

Before jumping into code, here’s what you’ll need:

  • ✅ A self-hosted WordPress site

  • ✅ WordPress REST API (enabled by default in latest versions)

  • ✅ A WordPress username + application password

  • ✅ Python 3 (installed locally or on server)

  • ✅ Python requests library


🔐 Step 1: Create a WordPress Application Password

  1. Log in to your WordPress admin

  2. Go to Users > Profile

  3. Scroll to Application Passwords

  4. Enter a name like PythonPoster and click Generate Password

  5. Copy the password (you won’t see it again!)

🛑 This password is used for secure authentication between Python and your WordPress site.


🧪 Step 2: Install Python Requests Library

If you don’t already have it installed:

bash
pip install requests

💻 Step 3: Python Script to Post to WordPress

Here’s a simple Python script to post a blog:

python
import requests
from requests.auth import HTTPBasicAuth
wp_url = “https://yourdomain.com/wp-json/wp/v2/posts”
username = “your_wp_username”
app_password = “your_app_password_here”

post = {
‘title’: ‘This is an Automated Post from Python’,
‘status’: ‘publish’,
‘content’: ‘This blog post was published using Python and the WordPress REST API.’,
‘categories’: [1], # Replace with your actual category ID
‘tags’: [3, 5] # Replace with tag IDs if needed
}

response = requests.post(wp_url, auth=HTTPBasicAuth(username, app_password), json=post)

print(“Status Code:”, response.status_code)
print(“Post Link:”, response.json().get(“link”))


📸 Image with Alt Text

Insert an image related to automation or Python with:

html
<img src="automate-posting.webp" alt="automate wordpress posting with python" />

🔗 External DoFollow Link

Learn more about the WordPress REST API here to understand all the available endpoints.

🖼️ Add Featured Image via Python

Want to upload a featured image automatically? Yes da, we can do that with another POST request using the WordPress Media API.

Automate WordPress Posting with Python


✅ Step 4: Upload and Attach Featured Image (Python Code)

python
import requests
from requests.auth import HTTPBasicAuth

media_url = “https://yourdomain.com/wp-json/wp/v2/media”
image_path = “your_image.jpg”

headers = {
‘Content-Disposition’: ‘attachment; filename=”your_image.jpg”‘,
‘Content-Type’: ‘image/jpeg’
}

with open(image_path, ‘rb’) as img:
media_upload = requests.post(
media_url,
auth=HTTPBasicAuth(“your_wp_username”, “your_app_password”),
headers=headers,
data=img
)

media_id = media_upload.json().get(‘id’)
print(“Image ID:”, media_id)

Now use that media_id in your post like this:

python
post = {
'title': 'Post with Image',
'content': 'This post has a featured image.',
'status': 'publish',
'featured_media': media_id
}

📁 Automate Posting from CSV or JSON

If you have multiple articles, use this logic to loop over them:

python

import csv

with open(“posts.csv”, newline=) as file:
reader = csv.DictReader(file)
for row in reader:
post = {
‘title’: row[‘title’],
‘content’: row[‘content’],
‘status’: ‘publish’,
‘categories’: [int(row[‘category_id’])],
‘tags’: [int(tag) for tag in row[‘tag_ids’].split(“,”)]
}

response = requests.post(
wp_url,
auth=HTTPBasicAuth(username, app_password),
json=post
)

print(“Posted:”, response.status_code, row[‘title’])

🟢 Perfect for posting ChatGPT-generated content automatically!


⏰ Schedule Posts in Advance

To schedule instead of publishing now:

python

from datetime import datetime, timedelta

scheduled_time = (datetime.utcnow() + timedelta(hours=3)).isoformat()
post[‘status’] = ‘future’
post[‘date_gmt’] = scheduled_time

This will schedule your post 3 hours later from current UTC time.

Automate WordPress Posting with Python


📎 Add Internal Links (from Bloggersurf.com)

You can add internal links directly in the content:

python
post['content'] = (
'Check out <a href="https://bloggersurf.com/ai-tools">our favorite AI tools</a> '
'or learn <a href="https://bloggersurf.com/schedule-social-media-posts-free">how to schedule social posts for free</a>.'
)

📌 Frequently Asked Questions (FAQ)

❓ Can I automate bulk blog posts with Python?

Yes, you can loop over a CSV, Google Sheet (via API)(), or even scrape data from a source and post automatically using the same logic.


❓ Is this method safe?

Yes. WordPress application passwords are secure and give limited access — better than using your main password.(Automate WordPress Posting with Python)


❓ Will this work for images, tags, and SEO?

Yes. You can upload images, assign tags/categories, and even set meta fields using plugins like Yoast or Rank Math API hooks.(Automate WordPress Posting with Python)

More about Automation

This Post Has One Comment

Leave a Reply