Created
September 27, 2023 07:35
-
-
Save NoahBohme/825ca33adf63d2f8b0c76f45f143475c to your computer and use it in GitHub Desktop.
Create a POST with the WordPress API using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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