WordPress REST API — Python

Matjaž Hozjan
2 min readSep 7, 2021

--

Type your WordPress address and after www.yourdomainname.com add wp-json/wp/v2/ and you will get build in REST API in json response.

That fancy future is great for posting articles via python or gathering data from the WP.

Photo by Douglas Lopes on Unsplash

Before getting deeper how to post via REST API and Python make sure that your .htaccess file allows you to write. My .htaccess for WordPress part looks like these below. (Please make backup 1st before you change anything and be sure that you know what you are doing.)

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Now we are ready to generate Application Passwords Name under Users/profile/Application Passwords. Input your wanted WordPress App on My Phone Name and click on Add New Application Password.

After you made new Application Password Name popup window will appear with a unique code. Please store it in a safe place. (In case that you didn’t save it. Delete old username and create new one.)

Now everything, that allows your python script to communicate with WP is set so is time to post to it with the code below.

For posting in right category, please make sure you set wanted category ID. At your WP go to categories and hoover up category with mouse and in left lower corner where full address is shown your category ID will appear.

Changing dates, titles etc is up to you and sky is the limit now.

Updating articles, collecting them and making other cool stuff is possible as well. Please check official WordPress tutorial for these.

--

--

Matjaž Hozjan
Matjaž Hozjan

Written by Matjaž Hozjan

FX, stock trader and full time DataScientist at SportRadar AG

Responses (1)