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.
Authentication with WordPress API
To interact with the WordPress API securely, you need to authenticate your requests. Let's delve into two common approaches:
Application Passwords
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.
- Log in to your WordPress admin dashboard.
- Navigate to Users → Profile.
- Scroll down to the "Application Passwords" section.
- Enter a name for the application (e.g., "API Access").
- Click "Add New Application Password".
- Copy the generated password (you won't be able to see it again).
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)
Basic Authentication Plugin
For older WordPress versions or if you prefer an alternative method:
- Download the Basic Authentication plugin from the WordPress.org GitHub repository.
- Install and activate the plugin on your WordPress site.
- Use your regular WordPress username and password for authentication.
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)
Publishing Posts at Specific Times
To schedule posts for publication at specific times, use the date
parameter when creating or updating a post. Here’s how:
Creating a Scheduled Post
import requests
from datetime import datetime, timedelta
url = "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)
Updating an Existing Post's Schedule
To reschedule an existing post, you’ll need its post ID:
import requests
from datetime import datetime, timedelta
post_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)
Important Notes
- Ensure your WordPress site is using HTTPS for secure communication.
- Keep your Application Password or regular password secure and never share it.
- The
date
parameter should be in ISO 8601 format (YYYY-MM-DDTHH:MM:SS). - The WordPress API uses UTC time, so adjust your scheduled times accordingly.
- Set the post
status
to "future" for scheduled posts. - You can also use the
date_gmt
parameter to specify the time in GMT/UTC directly.
By following this guide, you should be able to authenticate with the WordPress API and schedule posts for specific publication times programmatically.
Citations:
- Authentication – REST API Handbook | Developer.WordPress.org
- WordPress REST API: How to Access, Use, & Secure It (Full Tutorial)
- WordPress REST API Authentication – WordPress plugin | WordPress.org
- A Beginner’s Guide to WordPress API Basics - GetDevDone Blog
- What is WP REST API and How to Secure It | WordPress Rest API
- WordPress REST API Authentication | WordPress Plugin
Top comments (0)