In this guide, we’ll explore how to authenticate with the WordPress API and schedule posts for specific publication times. These steps will help you manage your WordPress content programmatically and securely.
To interact with the WordPress API securely, you need to authenticate your requests. Let's delve into two common approaches:
Application Passwords is a built-in feature in WordPress that allows you to generate secure passwords for API access without compromising your main account password.
To use the Application Password:
import requests
url = "https://your-wordpress-site.com/wp-json/wp/v2/posts"
username = "your_username"
app_password = "your_application_password"headers = {
"Content-Type": "application/json"
}response = requests.get(url, auth=(username, app_password), headers=headers)
For older WordPress versions or if you prefer an alternative method:
import requests
url = "https://your-wordpress-site.com/wp-json/wp/v2/posts"
username = "your_username"
password = "your_password"headers = {
"Content-Type": "application/json"
}response = requests.get(url, auth=(username, password), headers=headers)
To schedule posts for publication at specific times, use the date parameter when creating or updating a post. Here’s how:
import requests
from datetime import datetime, timedeltaurl = "https://your-wordpress-site.com/wp-json/wp/v2/posts"
username = "your_username"
app_password = "your_application_password"# Schedule the post for 2 days from now at 10:00 AM
scheduled_time = datetime.now() timedelta(days=2)
scheduled_time = scheduled_time.replace(hour=10, minute=0, second=0, microsecond=0)
scheduled_time_str = scheduled_time.isoformat()data = {
"title": "Scheduled Post Example",
"content": "This is the content of the scheduled post.",
"status": "future",
"date": scheduled_time_str
}response = requests.post(url, auth=(username, app_password), json=data)
if response.status_code == 201:
print("Post scheduled successfully!")
else:
print("Error scheduling post:", response.text)
To reschedule an existing post, you’ll need its post ID:
import requests
from datetime import datetime, timedeltapost_id = 123 # Replace with the actual post ID
url = f"https://your-wordpress-site.com/wp-json/wp/v2/posts/{post_id}"
username = "your_username"
app_password = "your_application_password"# Reschedule the post for 1 week from now at 2:00 PM
new_scheduled_time = datetime.now() timedelta(weeks=1)
new_scheduled_time = new_scheduled_time.replace(hour=14, minute=0, second=0, microsecond=0)
new_scheduled_time_str = new_scheduled_time.isoformat()data = {
"status": "future",
"date": new_scheduled_time_str
}response = requests.post(url, auth=(username, app_password), json=data)
if response.status_code == 200:
print("Post rescheduled successfully!")
else:
print("Error rescheduling post:", response.text)
By following this guide, you should be able to authenticate with the WordPress API and schedule posts for specific publication times programmatically.
Citations:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3