goodpress (or badpress?)
Write to WordPress, from R Markdown, with a modern stack.
The goal of goodpress is to post to WordPress from R Markdown. This is mostly a prototype since I don’t use WordPress myself. I need the prototype for a course.
Important disclaimer: I don’t use WordPress, so I am not sure you should trust me. You are welcome to try out the package (not on important stuff, rather in a playground of some sort), to contribute, and to volunteer to take over this package/concept. If you write a newer and better R package please tell me about it so I can add a link to it.
Installation
You can install the released version of goodpress from this repository:
# install.packages("remotes")
remotes::install_github("maelle/goodpress", ref = "main")
Authentication
From WordPress point-of-view this package is a “remote application” therefore it needs your website to use an authentication plugin. At the moment, for the sake of simplicity, this package only relies on Application Passwords.
You cannot install plugins if you use wordpress.com (unless you have a costly business plan there), therefore with wordpress.com you cannot use the REST API. There are paid services out there providing a domain name, hosting and a one-click WordPress install, for a few dollars a month, that you could use if you don’t roll your own server.
Here’s what I did to be able to use this package on my test website:
- Installed and activated the Application Passwords plugin.
- Created a new user with editor rights, not admin, and from the admin panel I created an application password for “rmarkdown” for that user.
- In
.Renviron
, save username asWP_USER
and password asWP_PWD
. - Edited the .htaccess file of my website
Syntax highlighting
For R
To get syntax highlighting for R blocks, at the moment you need to add custom CSS.
- Find
system.file(file.path("css", "code.css"), package = "goodpress")
and copy it to your clipboard. - From your WordPress admin dasbhoard, go to Appearance > Customize > Additional CSS. Paste the CSS there and click on publish.
You could edit colors that are in the CSS file.
Later I hope to make this process easier, maybe by adding inline styles.
Other languages
I haven’t explored that yet.
Workflow
- Create your posts in folders, one folder per post, with index.Rmd knitted to index.md and figures under a “figs” folder (so they can be different from non R related media).
fs::dir_tree(system.file(file.path("post-example2"), package = "goodpress"))
#> /home/maelle/R/x86_64-pc-linux-gnu-library/3.6/goodpress/post-example2
#> ├── chicago-fullnote-bibliography.csl
#> ├── figs
#> │ ├── pressure-1.png
#> │ ├── unnamed-chunk-1-1.png
#> │ └── unnamed-chunk-2-1.png
#> ├── index.Rmd
#> ├── index.md
#> └── refs.bib
- The post index.Rmd should use
hugodown::md_document
as an output format. - Knit your post and then, run the function
wp_post()
that takes the path as argument, create a draft post in your website, uploads all image media stored in the “figs” folder, edits the references to image media and then publishes the post. - The first time you run
wp_post()
in a folder, it creates a file called.wordpress.yml
that contains, in particular, the URL and ID of the post on your WordPress website. This way, next time the function is run, the post is updated.
Example post and its source. Note that it includes citations as footnotes by using the same strategy as hugodown.
The “one post per folder” thing is inspired by Hugo leaf bundles.
On disk your post is stored as index.Rmd and index.md, but before upload to the WordPress API it is transformed to HTML using Pandoc.
Motivation
The current best tool for writing from R Markdown to WordPress, knitr::knit2wp()
, relies on a package that hasn’t been updated in years and that depends on the no longer recommended RCurl
and XML
. In the meantime, WordPress gained a REST API that to my knowledge isn’t wrapped in any working R package.
There is also the solution to use a plug-in to sync a GitHub repo with a WordPress blog (see this website and its source) but it doesn’t handle media. If you use a GitHub repo:
- You could set up something like a GitHub Action that’d interact with WordPress REST API each time you push to the default branch.
- Are you still sure you don’t want to use a static website generator instead?
😉 More seriously, I am interested in blogging workflows so feel free to tell me why you use WordPress (in an issue for instance).