Skip to content

Instantly share code, notes, and snippets.

@ryanscharf
Forked from kalendar/wordpress-wjt-httr.R
Created July 20, 2019 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanscharf/cd9004d375321a0c79c78b709e5e57c5 to your computer and use it in GitHub Desktop.
Save ryanscharf/cd9004d375321a0c79c78b709e5e57c5 to your computer and use it in GitHub Desktop.
Publishing to the Wordpress REST API from R with JSON Web Tokens and httr
library(httr)
# Get the JWT
get_jwt <- POST("https://example.com/throwaway/wp-json/jwt-auth/v1/token",
query = list(username = "your_username", password = "your_password"))
token <- content(get_jwt)$token
# Confirm auth is working
test <- POST("https://example.com/throwaway/wp-json/jwt-auth/v1/token/validate",
add_headers('Authorization' = paste("Bearer", token, sep = " ")), encode = "json")
content(test)
# Publish page
create_page <- POST("https://example.com/throwaway//wp-json/wp/v2/pages",
add_headers('Authorization' = paste("Bearer", token, sep = " ")),
body = list(title = page_title, content = payload, status = "published"), encode = "json")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment