How to Automate a WordPress Post with Python

This post was created automatically by running a Python script!

1. In WordPress, go to Users > click admin > at the bottom type in a New Application Password Name, click Add New Application Password, and then a password will be generated.

2. In the Python script, set this password on the password = line.

How to Automate a WordPress Post
3. In the Python script, change the URL url = “https://coderfairy.com/wp-json/wp/v2”to your website. Also make sure that you change it to either https or http depending if you have a certificate installed or not.

4. Save an image named automate-wordpress-post.png to the same location as the Python script.

5. Run the Python script to automatically upload the text and image content to your WordPress post.

				
					import requests
import json
import base64

url = 'https://coderfairy.com/wp-json/wp/v2'
user = 'admin'
password = 'oa5D 5LSk Y7xq l57E PWoS Y8is'
creds = user + ':' + password
token = base64.b64encode(creds.encode())
header = {'Authorization':  'Basic ' + token.decode('utf-8')}

media = {
    'file': open('automate-wordpress-post.png', 'rb'),
    'caption': 'How to Automate a WordPress Post',
    'description': 'How to Automate a WordPress Post'
}

image = requests.post(url + '/media', headers=header, files=media)
imageURL = str(json.loads(image.content)['source_url'])

post = {
    'date': '2023-04-21T09:00:00',
    'title': 'How to Automate a WordPress Post with Python',
    'content': 'This post was created automatically by running a Python script!<br><br>' \
        '1. In WordPress, go to Users > click admin > at the bottom type in a <b>New Application Password Name</b>, click <b>Add New Application Password</b>, and then a password will be generated.<br><br>' \
        '2. In the Python script, set this password on the <b>password = </b> line.<br><br>' \
        '<figure class="wp-block-image"><img decoding="async" src="' + imageURL + '" alt="How to Automate a WordPress Post"></figure>' \
        '3. In the Python script, change the URL <b>url = "https://coderfairy.com/wp-json/wp/v2"</b>to your website.  Also make sure that you change it to either <b>https</b> or <b>http</b> depending if you have a certificate installed or not.<br><br>' \
        '4. Save an image named automate-wordpress-post.png to the same location as the Python script.<br><br>' \
        '5. Run the Python script to automatically upload the text and image content to your WordPress post.<br><br>',
    'status': 'publish'
}

r = requests.post(url + '/posts', headers=header, json=post)
print(r)
				
			

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top