Skip to content

Instantly share code, notes, and snippets.

@NoahBohme
Created September 27, 2023 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NoahBohme/825ca33adf63d2f8b0c76f45f143475c to your computer and use it in GitHub Desktop.
Save NoahBohme/825ca33adf63d2f8b0c76f45f143475c to your computer and use it in GitHub Desktop.
Create a POST with the WordPress API using Python
import requests
from requests.auth import HTTPBasicAuth
# Replace with your WordPress site URL
wordpress_url = 'https://website/wp-json/wp/v2/posts'
# Replace with your username and password
username = 'user'
password = 'lXsJ J1jB R43j MaBq lkzP x5Hr'
# Create a new post data
post_data = {
'title': 'New Post Title',
'content': 'This is the content of the new post.',
'status': 'publish' # You can also use 'draft' or 'private'
}
# Make a POST request to create the new post with Basic Authentication
response = requests.post(
wordpress_url,
json=post_data,
auth=HTTPBasicAuth(username, password)
)
# Check the response status code
if response.status_code == 201:
print('Post created successfully!')
print('Post ID:', response.json()['id'])
else:
print('Failed to create the post. Status code:', response.status_code)
print('Response:', response.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment