S Lazy-H
  • Home
  • About
  • Posts
  • Contact
  • Slide Rules
  • A Biker’s Tale

Posting to Wordpress

R Programming
Author

Sam Hutchins

Published

April 2, 2020

An earlier post alluded to posting to Wordpress briefly in conjunction with another subject. So to round things out a bit more, I will show what I am using to post to a Wordpress site.

I am using some R packages (RWordPress, knitr) to accomplish this. I had earlier found a script in the Internet to do this too, but it was rather old (2017) so did not work with the version I have installed on my computer from source (Version 3.6.2, “Dark and Stormy Night”). I have been installing from source for several versions as I needed the toolchain anyway, and it was fun!

The latest version available for Fedora 31 is 3.6.3 (Holding the Windsock) binary package. However, the 4.0.0 (Arbor Day) version is slated for release tomorrow, Friday. So I will see if I can install it then. Prerelease versions can be found from the CRAN.R-Project.

Anyway, to upload to Wordpress, I am using the script below. It is placed as a draft so I can then login and change whatever I need before I publish it.


library(RWordPress)
library(knitr)

print("This is to upload a '.rmd' file to WordPress as a draft post. Prepare the rmd file in the Posts directory.")
pw <- scan("Path/to/password.txt", what=character())
title <- readline(prompt="Enter the post title: ")
print("Available files:")
system("ls Posts")
file <- readline(prompt="Enter .rmd filename to upload: ")
file <- paste("Posts/", file, sep="")
options(WordpressLogin = c(user=pw), WordpressURL = "https://website.com/xmlrpc.php")

knit2wp(file, title, publish = FALSE)

After writing the actual file I wish to post as a ‘postname.rmd’ file, I then, in the script, ask for a title for the post, show the files available for selection, then perform the action. Pretty simple!
Now, because Wordpress finds image files either through uploads or from the media library, any images included in the post don’t make the journey, just a ‘code chunk’ message at the place in the post. I have not yet figured how to make that not happen. Obviously, more work and research is needed on my part to be successful for that.

© S Lazy-H 2019 -