WordPress uses REST API to create, update, and delete articles tutorial (Python code)

888u

Last update at :2024-03-23,Edit by888u

When I introduced WordPress application passwords before, this site mentioned that one of the major uses of this program password is to authorize the REST API. Today I will introduce to you the usage of the WordPress REST API and use Python to create and update it. , delete the article.

1. Apply for WordPress application password

The WordPress application password is not the login password. For detailed introduction and application methods, please refer to the previous sharing on this site:

  • "WordPress application passwords setting tutorial"

2. Use REST API to manage blog posts

The Python version of the code is shared directly here. Other codes can be modified on this basis.

First you need to import the required packages:

import requests import json import base64

You can use this code to check whether the authorization is successful and whether the response is correct:

url = "https://example.com/wp-json/wp/v2/posts" user = "your-username" password = "your-application-password" credentials = user + ':' + password token = base64.b64encode(credentials.encode()) header = {'Authorization': 'Basic ' + token.decode('utf-8')} response = requests.get(url, headers=header) print(response)

Among them:

  • example.com is your own domain name
  • user is your login name
  • password is the application password you just applied for

Use REST API to create new article code:

url = "https://example.com/wp-json/wp/v2/posts" user = "your-username" password = "your-application-password" credentials = user + ':' + password token = base64.b64encode(credentials.encode()) header = {'Authorization': 'Basic ' + token.decode('utf-8')} post = { 'title' : 'Hello World', 'status' : 'publish', 'content' : 'This is my first post created using rest API', 'categories': 5, // category ID 'date' : '2020-01-05T10:00:00' } response = requests.post(url, headers=header, json=post) print(response)

Use REST API to update article code:

url = "https://example.com/wp-json/wp/v2/posts/" postID=1 user = "your-username" password = "your-application-password" credentials = user + ':' + password token = base64.b64encode(credentials.encode()) header = {'Authorization': 'Basic ' + token.decode('utf-8')} post = { 'title' : 'Hello World Updated', 'content' : 'This is my first post created using rest API Updated' } response = requests.post(url + postID, headers=header, json=post) print(response)

Use REST API to delete article code:

url = "https://example.com/wp-json/wp/v2/posts/" user = "your-username" password = "your-application-password" credentials = user + ':' + password token = base64.b64encode(credentials.encode()) header = {'Authorization': 'Basic ' + token.decode('utf-8')} response = requests.delete(url + postID, headers=header) print(response)

The above is how to use the WordPress REST API. In addition to this interface, WordPress also has an xmlrpc interface, which can also be used to operate the article: "Python uses xmlrpc to operate WordPress: publish articles, create new tags and categories"

Recommended site searches: US virtual host, permanent free Linux server, domain name purchase, mainland China proxy IP, server hosting, Korean virtual host ICP registration inquiry, free foreign space, US server URL, website virtual host space,

WordPress uses REST API to create, update, and delete articles tutorial (Python code)

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码