How to Publish content in WordPress using API in Python ?

How to Publish content in Wordpress using API in Python _

We can Publish content in WordPress using API in Python with WordPress JSON endpoint and Application Password. In this article, we will practically show the step-by-step solution for this. The only thing we will highlight first is here we require an application password to perform any action with WordPress API.  Usually, login passwords become vulnerable and become a loophole for hackers. But as per security principle application password is one-time generated and no one can recover it again until we share it manually with any application.

In this article, we will draft an article with Python and WordPress API step by step.

Publish content in WordPress using API in Python –

Here is the first step.

Step 1 : Generating Application Password –

We need to login in the WordPress and then navigate to the left pane user section. Here we select the use and scroll below. Refer the below screenshot for more details.

application password
application password

Step 2 :Credential Setup –

In this step, we need to setup three Python variables. Here are their details.

user_id = "ABCD"
user_app_password = "XYZ"
wp_url = "https://YOURWEBSITE.com/wp-json/wp/v2"

here user_id,user_app_password,wp_url need to be set as your config.

Step 3: Execute the below code –

In this section, we need to change the final_content, title, and status variable primarily. Even if you keep other factors the same you can publish or store the article in draft.

wp_post_url = wp_url + "/posts"
wp_media_url = wp_url + "/media"
credentials = user_id + ':' + user_app_password
token = base64.b64encode(credentials.encode())
header = {'Authorization': 'Basic ' + token.decode('utf-8')}
final_content='Article Text what you want to publish keep in this string only'
post_data = {
  "title": "Keep your article tiltle",
  "content": final_content,
  "comment_status": "closed",
  "categories": [1],
  "status": "draft",
  "featured_media": 0
 }
try:
  response = requests.post(wp_post_url,headers=header, json=post_data)
except:
  print ("Error!")
  response = ""
print(response.text)

Let’s verify whether is our implementation working or not. For this, we will open the WordPress portal for which we have configured. You can open your portal accordingly and go to the draft.

wordpress post using python api
WordPress post using Python API

Here we can see that our article is in draft.

Thanks

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner