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:
<p>import requests</p> <p>url = "https://your-wordpress-site.com/wp-json/wp/v2/posts"<br> username = "your_username"<br> app_password = "your_application_password"</p> <p>headers = {<br> "Content-Type": "application/json"<br> }</p> <p>response = requests.get(url, auth=(username, app_password), headers=headers)</p>
For older WordPress versions or if you prefer an alternative method:
<p>import requests</p> <p>url = "https://your-wordpress-site.com/wp-json/wp/v2/posts"<br> username = "your_username"<br> password = "your_password"</p> <p>headers = {<br> "Content-Type": "application/json"<br> }</p> <p>response = requests.get(url, auth=(username, password), headers=headers)</p>
To schedule posts for publication at specific times, use the date parameter when creating or updating a post. Here’s how:
<p>import requests<br> from datetime import datetime, timedelta</p> <p>url = "https://your-wordpress-site.com/wp-json/wp/v2/posts"<br> username = "your_username"<br> app_password = "your_application_password"</p> <p># Schedule the post for 2 days from now at 10:00 AM<br> scheduled_time = datetime.now() + timedelta(days=2)<br> scheduled_time = scheduled_time.replace(hour=10, minute=0, second=0, microsecond=0)<br> scheduled_time_str = scheduled_time.isoformat()</p> <p>data = {<br> "title": "Scheduled Post Example",<br> "content": "This is the content of the scheduled post.",<br> "status": "future",<br> "date": scheduled_time_str<br> }</p> <p>response = requests.post(url, auth=(username, app_password), json=data)</p> <p>if response.status_code == 201:<br> print("Post scheduled successfully!")<br> else:<br> print("Error scheduling post:", response.text)</p>
To reschedule an existing post, you’ll need its post ID:
<p>import requests<br> from datetime import datetime, timedelta</p> <p>post_id = 123 # Replace with the actual post ID<br> url = f"https://your-wordpress-site.com/wp-json/wp/v2/posts/{post_id}"<br> username = "your_username"<br> app_password = "your_application_password"</p> <p># Reschedule the post for 1 week from now at 2:00 PM<br> new_scheduled_time = datetime.now() + timedelta(weeks=1)<br> new_scheduled_time = new_scheduled_time.replace(hour=14, minute=0, second=0, microsecond=0)<br> new_scheduled_time_str = new_scheduled_time.isoformat()</p> <p>data = {<br> "status": "future",<br> "date": new_scheduled_time_str<br> }</p> <p>response = requests.post(url, auth=(username, app_password), json=data)</p> <p>if response.status_code == 200:<br> print("Post rescheduled successfully!")<br> else:<br> print("Error rescheduling post:", response.text)</p>
By following this guide, you should be able to authenticate with the WordPress API and schedule posts for specific publication times programmatically.
Citations:
The above is the detailed content of A Comprehensive Guide to Using the WordPress API: Authentication and Post Scheduling. For more information, please follow other related articles on the PHP Chinese website!