Giter Site home page Giter Site logo

wordpress-rest-api-with-python's Introduction

wordpress with python

Example of WordPress REST API with Python. we will fetch all posts, create one new post, update it and delete it using REST API with Python.

Authentication ๐Ÿ”’

First of all, we need basic authentication for performing crud operation and it wonโ€™t work until you install a special plugin named application password to get the application password. Go to your dashboard -> plugin -> Add new and type application password in the search box.

Once you installed plugin go to your profile settings and scroll to the bottom. You will see one application password field will be added there. Type you application name and hit enter and popup will appear with a password. Copy generated password from popup.

Itโ€™s time to go through the python ๐Ÿ‘ฝ

Now, Create one file with the .py extension. You can give it any name (for example app.py).

Import some required packages:
import requests
import json
import base64

Fetch Posts ๐Ÿ“ฎ

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')}
responce = requests.get(url , headers=header)
print(responce)

Letโ€™s understand this code. URL is the URL of the site from which you want to fetch posts. user is your admin username. password is the application password that you have generated just before. Encode your credentials and pass them in header authorization as shown in code. At the bottom of the line we are printing our response to see what we are getting. Now, open your terminal and run your app.py file. python app.py

Create Post ๐Ÿ“ฎ

To create a post we need to make the post request with some required parameters like URL, your JSON data. Letโ€™s do that:

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'
}
responce = requests.post(url , headers=header, json=post)
print(responce)

Update Post ๐Ÿ“ฎ

To update post you need to pass post id to tell REST API which post you want to update. Letโ€™s see how to do that.

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'
}
responce = requests.post(url + postID , headers=header, json=post)
print(responce)

Here, postID is the id of the post you want to update.

Delete Post ๐Ÿ“ฎ

To delete a post we need to use delete requests provided by python.

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')}
responce = requests.delete(url + postID , headers=header)
print(responce)

Useful Blogs:

  1. Create Single Page Application With React js and WordPress REST API
  2. How to create custom Gutenberg block as a begginer
  3. WordPress REST API With Python

Please follow me on Medium and github ๐Ÿ™

wordpress-rest-api-with-python's People

Contributors

brdhanani avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.