How to publish a blog post on Wordpress using Rmarkdown

(c)cc0, from pixabay.com

I like WordPress and I love Rmarkdown. So recently I’ve thought: With Rmarkdown I can easily produce HTMLs of analyses, research reports, or tutorials. My blog runs on WordPress. So instead of manually copying everything over to WordPress, wouldn’t it be nice if I could automatically upload my Rmarkdown scripts to WordPress … ?

So I checked for existing solutions and, much to my surprise, it’s possible! With this blogpost, I hence …

  1. want to share my joy that this option exists,
  2. show you an example of how you can do it yourself,
  3. mention some obstacles I’ve wrestled with so that you don’t have to.

This is great!

As Heather Urry has already written in her post on using Rmarkdown and WordPress, some people are simply amazing. In order to connect Rmarkdown and WordPress, Duncan Temple Lang developed the R package RWordPress and William Morris, Yihui Xie, and Jared Lander the knit2wp function in knitr. So thank you very much for making this possible!

(In general, I think it’s hard to underestimate the epistemic contribution of people such as Frederik Aust (papaja), Sascha Epskamp (semPlot), Dominik Leiner (soscisurvery), Michelle Nuijten (statcheck), Yves Rosseell (lavaan), Hadley Wickham (tidyverse), or Yihui Xie (knitr). These people have created software that is free-to-use, open source, and user-friendly. Their direct and indirect contribution is enormous. Which makes it even more heartbreaking that some software-developing academics are often not taken seriously, that their work is overlooked, or that their products are not considered to be of scientific value. This has to change.)

Example

It’s straightforward: You only need to write you Rmarkdown file as you would normally do. Let’s call this file “post.Rmd”. You’d then create another file, for example called “publish.R”. You run the code in the latter and, voila, your post is published!

The publish file should include the following lines:

if (!require('knitr')) {install.packages("knitr")}
if (!require('devtools')) {install.packages("devtools")}
if (!require('RWordPress')) {devtools::install_github(c("duncantl/XMLRPC", "duncantl/RWordPress"))}

library(knitr); library(RWordPress)

options(WordpressLogin = c(my_user_id = 'PASSWORD'),
        WordpressURL = 'http://YOURWEBSITE.com/xmlrpc.php')

knit2wp('post.RmD',
        title = 'YOUR TITLE',
        publish = FALSE,
        action = "newPost")

First, you would set the parameter action to “newPost”. You can later change this to “editPost”. When editing your post, you would then need to add a postid (which you can find in your console after you’ve published the first version).

I’m running WordPress on my own webspace. But several people might have a blog directly on wordpress.com. In this case, change WordpressURL to “http://YOURWEBSITE.wordpress.com/xmlrpc.php”.

Plus, also make sure to change my_user_id to your actual id, together with the corresponding password (thanks to Nick who mentioned that in the comment).

You don’t have to create a separate publish.RmD file. But it’s a good idea if you’re planning on uploading your code to a public repository such as github. You can then easily add that file to your .gitignore – which makes much sense, because you probably don’t want to share your password with the rest of the world.

For an up and running example, you can see my latest blog post What is statistical power? An illustration using simulated data, which was written in Rmarkdown and then published in WordPress. You can find the code on my github. Likewise, you can also find the code for this blogpost online.

Implement custom CSS/HTML

Some things didn’t work for me in the beginning. This meant including footnotes, the Read More feature of WordPress, or the embedding of external media. However, I’ve figured that it’s easy to implement all that that as well!

Footnotes

In Rmarkdown, you’d implement footnotes as follows:

Main text^[1] ^[1]: Footenote

However, this doesn’t work when exporting to WordPress. However, you can make it work by implementing custom snippets of CSS/HTML.1 You only need to install a footnote plugin, in my case Easy Footnotes, and add the HTML code to your Rmarkdown file. For example like this:

This is text.
[Note]
This is your footnote.
[/Note]
This is text.

Read More

Next, in WordPress there’s a Read More option. This feature enables that in the overview of all your blog posts, an individual blogsposts isn’t shown completely, but only up to that mark. Obviously, that feature doesn’t exist in Rmarkdown. But, again, you can implement it by adding the following lines of code to your Rmarkdown:

<pre><code class="r">
<!-- wp:more -->
<!--more-->
<!-- /wp:more -->
</pre></code class="r">

Embed Media

As you’ve gathered by now, the general idea is that you would need to find out how the HTML code for something looks like and then simply add it manually to your Rmarkdown file. This way, we can for example implement the WordPress style to embed external media:

The source you’d need to use would be the following:

<!-- wp:core-embed/twitter {"url":"https://twitter.com/tdienlin/status/1103692112158212096?s=20","type":"rich","providerNameSlug":"twitter"} -->
<figure class="wp-block-embed-twitter wp-block-embed is-type-rich is-provider-twitter"><div class="wp-block-embed__wrapper">
# https://twitter.com/tdienlin/status/1103692112158212096?s=20
</div></figure>
<!-- /wp:core-embed/twitter -->

Easy, right?

One thing I haven’t yet managed to get to work are references. If you know how, please let me know!

Trouble-Shooting

Some things didn’t work in the beginning. I had a hard time actually connecting to my WordPress server. The first problem was that several online tutorials simply featured wrong code. Pro tip: If you should have problems to connect, make sure that “WordpressLogin” and “WordpressURL” are spelled correctly!

Next, as I’m hosting wordpress on an external server, I did not have the permission to open the “xmlrpc.php” file. It took me some time to find out how to change that. For me, the solution was to add the following lines of code to my .htaccess file:

<FilesMatch "xmlrpc\.php$">
Satisfy Any
Allow from all
</FilesMatch>

If you do that, it’s important to also install the plugin “Disable XML-RPC Pingback”, so that others cannot hack your page. If you should have the same problem and you cannot fix it, you might find an answer in one of these sites, which I found very helpful:

In general, for more information and examples, see the following pages:

Happy blogging!

Like this post on social media

Footnotes

  1. This is the proof.

14 Comments

  1. Hi Tobias,

    I have been using RWordpress for a while but out of the blue, I am getting this error:

    Error in xml.rpc(.server, method, as.character(blogid), names(login), :
    Problems

    Are you familiar with this?

    1. Hi Dan, hm, nope, that’s new to me. I currently don’t have the time to delve into this, but will try to do so in the upcoming days!

  2. Hi Tobias, thank you for the post: interesting and useful!
    I have lost some time trying to make the code work, until I realized that ‘WordPressLogin’ and ‘WordPressURL’ should be ‘WordpressLogin’ and ‘WordpressURL’, respectively. That is, the arguments’ names are case-sensitive and it is a lowercase p in WordPress. I just thought you may wish to know. Thank you for sharing your joy with all of us!

    1. Thanks you very much for letting me know. Crazy thing is: My own post/html had the correct spelling (i.e., Wordpresslogin), but wordpress by default and deliberately changed this to WordPress using a php-script! (See following post: https://clarkwp.wordpress.com/2014/04/30/why-is-the-p-in-wordpress-important/). So I needed to install an additional plugin that disables this “feature” so that now the code is displayed correctly 😅 Wow. So thanks again, very helpful comment!

  3. Hi, I followed the tutorial but somehow I got this error:

    Error in function (type, msg, asError = TRUE) :
    Failed to connect to localhost port 443: Connection refused.

    Have you ever experienced the same problem? Or do you have any idea why this error happened? Any answer would be highly appreciated. Thanks in advance.

    1. I think this could be anything, but probably also due to you not having permission to update the “xmlrpc.php” file. I’ve updated the section “trouble-shooting”, bc. there were some formatting errors. Maybe what I’m outlining there helps?

  4. output file: post.md

    Error in function (type, msg, asError = TRUE) :
    error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version

    1. Do you have any idea how to get around with this issue? Doesnt allow to post from Rstudio
      output file: post.md

      Error in function (type, msg, asError = TRUE) :
      error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version

      1. No, I’m sorry, I did not encounter this problem, and don’t really see a quick fix what to do about it. Sorry!

  5. This seems like a dump question, but the line:
    options(WordpressLogin = c(user = ‘PASSWORD’),

    Leaves me wondering where to specify the userid that is associated with the password and thus with the WP blog site.

  6. Hi,
    I’ve achieved to send my first r script from rstudio to wordpress. But I couldn’t see the output graphics. How can I solve this problem?
    BR,

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.